Question

You will create a class to keep student's information: name, student ID, and grade. The program...

You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality:
- The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change.
- The program should provide the option to create a new entry with a student's name, ID, and grade.
- There should be an option to lookup a student from his student ID (this will be the key in the hash table).
- There should be an option to remove a particular entry by providing the student ID.
- There should be an option to display the whole registry, sorted by student ID.
- Remember that the registry must be persistent, so you will have to save all this information to the file system. You may use any of the versions of hash tables implemented in the previous exercise.

Here is my code for it so far, it might not be correct.

public class Student<String, V> extends HashTable<String, V> {
   private V name;
   private String id;
   private int grade;
  
   public V lookup() {
       HashTable<String, V> registry = new HashTable<String, V>();
       Scanner input = new Scanner(System.in);
       System.out.print("Please Enter the Student ID: ");
       id = (String) input.next();
       registry.lookup(id);
       java.lang.String out = "The Student's Name is " + name;
       return (V) out;
   }
  
   public V remove() {
       HashTable<String, V> registry = new HashTable<String, V>();
       Scanner input = new Scanner(System.in);
       System.out.print("Please Enter the Student ID: ");
       id = (String) input.next();
       super.remove(id);
       java.lang.String out = "The Student That Has Been Removed is " + name;
       return (V) out;
   }
  
   public java.lang.String display() {
       HashTable<String, V> registry = new HashTable<String, V>();
       super.getSortedList(null);
       System.out.println(something);
   }
  
   public static void main(java.lang.String[] args) {
       HashTable<String, V> registry = new HashTable<String, V>();
       Scanner input = new Scanner(System.in);
       java.lang.String choice;
       System.out.print("\nWhat Would You Like to Do? Enter a Number:"
                           + "\n1. Lookup Student by ID"
                           + "\n2. Remove an entry with Student ID"
                           + "\n3. Display Registry\n");
       switch (choice) {
       case "1":
           registry.lookupName();
       case "2":
           registry.removeEntry();
       case "3":
           registry.display();
       }
      
   }
}

The 'key' is the student id, and the 'value' is to call the object Student to get the name and letter grade.

I don't understand how to make the registry "persistent'.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Student.java
import java.util.*;
import java.io.*;
public class Student {
   private String name,grade;
   int id;
   //constructer

   public Student(String name,int id,String grade){
       this.name=name;
       this.id=id;
       this.grade=grade;
   }
  
   public int getId(){
       return id;
   }
   //getting student
   public String toString()
   {
       return this.id + " "+ this.name+" "+this.grade;
   }
   //main method
   public static void main(String args[]){
       Hashtable<Integer,Student> register=new Hashtable<Integer,Student>(); //hashtable
       Scanner input = new Scanner(System.in); //input
       int option;
       int id;
         while(true){
           System.out.println();
           //menu
           System.out.print("\nWhat Would You Like to Do? Enter a Number:"
                           +"\n1. Inserting new Student"  
                            + "\n2. Lookup Student by ID"
                            + "\n3. Remove an entry with Student ID"
                            + "\n4. Display Registry"
                            +"\n5.exit\n");
           option=input.nextInt();
           if(option==5){
               break;
           }
           switch(option){
               case 1:
                   Student newStudent=createStudent();
                   register.put(newStudent.getId(),newStudent);
                   try{
                       writeToFIle(register); //writing to file
                   }
                   catch(Exception e){
                   }
                   break;
               case 2:
                   System.out.print("Please Enter the Student ID: ");
                   id =input.nextInt();
                   //found
                   if(register.get(id)!=null){
                       System.out.println(register.get(id));
                   }
                   else{
                       System.out.println("Student Not found");
                   }
                   break;
               case 3:
                    System.out.print("Please Enter the Student ID: ");
                   id = input.nextInt();
                   if(register.get(id)!=null){
                       register.remove(id);//removing student
                       try{
                           writeToFIle(register); //writing to file
                       }
                       catch(Exception e){
                       }
                   }
                   else{
                       System.out.println("Student Not found");
                   }


                   break;
               case 4:
               //displaying register
                    List<Integer> v = new ArrayList<Integer>(register.keySet());
                   Collections.sort(v);

                   for (Integer str : v) {
                      System.out.println(str + " " + register.get(str));
                   }
                   break;
           }
       }              
           try{
               writeToFIle(register);
           }
           catch(Exception e){
           }
   }
   public static Student createStudent(){
       String name,grade;
       int id;
       Scanner sc=new Scanner(System.in);
       System.out.printf("Enter id::");
       id=sc.nextInt();
       System.out.printf("Enter Name::");
       name=sc.next();
       System.out.printf("Enter Grade::");
       grade=sc.next();
      
       return new Student(name,id,grade);
   }
   public static void writeToFIle(Hashtable<Integer,Student> h) throws IOException{
          List<Integer> v = new ArrayList<Integer>(h.keySet());
           Collections.sort(v);
          String result="";
          for (Integer str : v) {
              result+=str + " " + h.get(str)+"\n";
           }
       
            File file = new File("register.txt");
    
          // creates the file
          file.createNewFile();
      
          // creates a FileWriter Object
          FileWriter writer = new FileWriter(file);
      
          // Writes the content to the file
          writer.write(result);
          writer.flush();
          writer.close();
          System.out.println("Record written to FIle");
   }
}

Add a comment
Know the answer?
Add Answer to:
You will create a class to keep student's information: name, student ID, and grade. The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would you make it hold both the student's name and their teacher? then be able...

    How would you make it hold both the student's name and their teacher? then be able to sort based on either the students name or who their teacher is? import java.util.Scanner; //for taking user input public class StudentName{ //you can change class name public static void main(String[] args); { Scanner in = new Scanner(System.in); //finding length of array studentNames System.out.print("how many students? :") ; int totalStudents = in. nextInt() ; String[] studentNames = new String[totalStudents] ; //input user name for(int...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Write an application that displays a series of at least five student ID numbers (that you...

    Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException, display the message Score over 100, and then store a 0 for the student’s score. At the...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • Complete the program, Grade.java, below. The program takes in a student's score and prints the corresponding...

    Complete the program, Grade.java, below. The program takes in a student's score and prints the corresponding letter grade assuming a standard 60-70-80-90 scale for cutoff of each grade. For example, if a student's score is 52.6 they would be assigned an "F" grade. If a student's score is 87.8 they would be assigned a "B grade. The program should take in the score as a double value and print the letter grade (capitalized letter of A, B, C, D, or...

  • Add additional information to the student class below such as name, address, major, and contact information...

    Add additional information to the student class below such as name, address, major, and contact information along with all the getters and setters (methods) needed to access all data. Submit your code along with a sample run of your program. Comment all your code. Cite any sources of information you use. Write a note on the process of completing the assignment in a Microsoft Word document. // ShowStudent.java // client to test the Student class class ShowStudent { public static...

  • Add additional information to the student class below such as name, address, major, and contact information...

    Add additional information to the student class below such as name, address, major, and contact information along with all the getters and setters (methods) needed to access all data. Submit your code along with a sample run of your program. Comment all your code. Cite any sources of information you use. Write a note on the process of completing the assignment in a Microsoft Word document. // ShowStudent.java // client to test the Student class class ShowStudent { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT