Question: JAVA - How do I do the StudentApp class and get the code to run by combining all classes? This is the requirements for the
JAVA - How do I do the StudentApp class and get the code to run by combining all classes? This is the requirements for the StudentApp class and this is the code for each of my classes.
After you have your Student and SinglyLinkedList classes coded and tested, you will write a class named StudentApp that represents an application to keep track of student information. Unlike the similar task in Assignment 2B, there is no need to ask for a maximum size of the data set. You should program this though so that the user will be asked for the initial number of students. You may either prompt them to input the initial data set, using the input method of the Student class for each or use the RandomStudents class provided to you in Assignment 1D to generate the initial data set. Once this is complete, the user will be presented with the following menu options:
Enter: 1 to insert a new student's information,
2 to fetch and output a student's information,
3 to delete a student's information,
4 to update a student's information,
5 to output all the student information, and
6 to exit the program.
This menu will be presented repeatedly until the user chooses option 6. Each of the options should call the methods that you wrote for your SinglyLinkedList structure. You should include code that provides feedback for successful completion of the operation and an appropriate error message if an operation fails.
import java.util.Scanner; import java.io.*; public class Student { private String name; private String id; private double gpa; public Student() { this(" ", " ", 0.0); } public Student(Student s) { name = s.name; id = s.id; gpa = s.gpa; } public Student(String name, String id, double gpa) { set(name, id, gpa); } public String toString() { return "Student: [Name: " + name + ", Id: " + id + " GPA: " + gpa + "]"; } public int compareTo(String targetKey) { return(name.compareTo(targetKey)); } public void input() { Scanner scanner = new Scanner(System.in); System.out.println("Enter Student name: "); name = scanner.nextLine(); System.out.println("Enter Student ID: "); id = scanner.nextLine(); System.out.println("Enter Student GPA: "); gpa = scanner.nextDouble(); scanner.nextLine(); } public void set(String name, String id, double gpa) { this.name = name; this.id = id; this.gpa = gpa; } public String getName() { return name; } public String getId() { return id; } public double getGpa() { return gpa; } public boolean equals(Object otherObject) { if (otherObject == null) { return false; } else if(getClass() != otherObject.getClass()) { return false; } else { Student otherStudent = (Student)otherObject; return id.equals(otherStudent.id); } } public void setName(String newName) { this.name = newName; } public Student deepCopy( ) { Student clone = new Student(name, id, gpa); return clone; } } public class Node { private Student record; private Node next; Node(Student newRecord) { record = newRecord; next = null; } public Student getRecord() { return record; } public void setRecord(Student newRecord) { record = newRecord; } public Node getNext() { return next; } public void setNext(Node newNext) { next = newNext; } public String toString() { return record.toString(); } } import java.util.*; import java.io.*; public class RandomStudents { // instance variables private String studentNames[]; private int numberOfNames; private Random randomInteger; public RandomStudents() { numberOfNames = 1000; studentNames = new String[numberOfNames*2]; readNamesFromFile("C:\\Data Structures and Algorithms/boynames.txt", 0, numberOfNames); readNamesFromFile("C:\\Data Structures and Algorithms/girlnames.txt", numberOfNames, numberOfNames*2); long seed = System.currentTimeMillis(); //Get current time as a long. randomInteger = new Random(seed);//Use seed to generate random numbers. } private void readNamesFromFile( String fileName, int start, int end) { Scanner inputStream = null; try { inputStream = new Scanner(new FileInputStream(fileName)); for (int i=start; i< end; i++) { String line = inputStream.nextLine(); //Parse out first name, ignore the number int space = line.indexOf(" ",0); studentNames[i] = line.substring(0,space); } inputStream.close(); } catch(FileNotFoundException e) { System.out.println("File: " + fileName + " not found"); System.out.println("or could not be opened."); System.exit(0); } catch (IOException e) { System.out.println("Error reading from file:" + fileName); System.exit(0); } } private String getRandomName( ) { int index = randomInteger.nextInt(numberOfNames*2); return studentNames[index]; } private String getRandomID( ) { int number = randomInteger.nextInt(100000000) + 100000000; String value = Integer.valueOf(number).toString( ); return "s" + value.substring( 1, value.length( )); } private double getRandomGPA( ) { int gpaInteger = randomInteger.nextInt(200) + 201; double gpaDouble = gpaInteger/100.0; return gpaDouble; } public Student getStudent( ) { String name = getRandomName( ); String id = getRandomID( ); double gpa = getRandomGPA( ); return new Student(name, id, gpa); } } import java.util.Scanner; import java.io .*; public class SinglyLinkedList { private static Scanner input = new Scanner( System.in ); private Node h; public SinglyLinkedList() { h = null; } public boolean insert(Student student) { if(student == null) { return false; } Node newStudent = new Node(student); newStudent.setRecord(student); newStudent.setNext(null); Node temp = null; if(h == null) { h = newStudent; } else { temp = h; } while(temp != null && temp.getNext() != null) { temp = temp.getNext(); } temp.setNext(newStudent); return true; } public Student fetch(String queryName) { Node temp = h; while(temp != null) { if(temp.getRecord().compareTo(queryName) == 0) return temp.getRecord(); temp = temp.getNext(); } return null; } public boolean delete(String name) { if(h.getRecord().compareTo(name) == 0) { h = h.getNext(); } else { Node curr = h, prev = null; while(curr != null && curr.getRecord().compareTo(name)!= 0) { prev = curr; curr = curr.getNext(); } if(curr == null) return false; prev.setNext(curr.getNext()); } return true; } public boolean update(String queryName, Student updatedStudent) { Node temp = h; while(temp != null && temp.getRecord().compareTo(queryName) != 0) { temp = temp.getNext(); } if(temp == null) return false; temp.setRecord(updatedStudent); return true; } public void showAll() { Node curr = h; while(curr != null) { System.out.println(curr); curr = curr.getNext(); } } } This is what I had for StudentApp but it is not working out for me... import java.util.Random; import java.util.Scanner; public class StudentApp{ static SinglyLinkedList students = new SinglyLinkedList(); public static void main(String[] args) { RandomStudents random = new RandomStudents(); Student newStudent = random.getStudent(); students.insert(newStudent); while (random.menuChoice != 6) random.DrawMenu(); } private int menuChoice = 0; private SinglyLinkedList data; private void DrawMenu() { System.out.println("Enter: 1. Insert a new student's information,"); System.out.println(" 2. Fetch and output a student's information,"); System.out.println(" 3. Delete a student's information,"); System.out.println(" 4. Update a student's information,"); System.out.println(" 5. Output all the students information in sorted order"); System.out.println(" 6. Exit the program"); Scanner scan = new Scanner(System.in); try { menuChoice = Integer.parseInt(scan.nextLine()); System.out.flush(); } catch (Exception e) { System.out.flush(); System.out.println("ERROR: Choose a number between 1-6 "); menuChoice = 0; return; } if (menuChoice == 1) { Student student = new Student(); student.input(); data.insert(student); } else if (menuChoice == 2) { System.out.print("Enter a student name: "); String nameToSearch = scan.nextLine(); System.out.println(data.fetch(nameToSearch)); } else if (menuChoice == 3) { System.out.print("Enter a student name: "); String nameToSearch = scan.nextLine(); System.out.println(data.delete(nameToSearch)); } else if (menuChoice == 4) { System.out.print("Enter a student name: "); String nameToSearch = scan.nextLine(); Student studentToChange = data.fetch(nameToSearch); System.out.println("Current GPA: " + studentToChange.getGpa()); System.out.print("Update GPA: "); double newGpa = Double.parseDouble(scan.nextLine()); data.delete(nameToSearch); data.insert(new Student(studentToChange.getName(), studentToChange.getId(), newGpa)); } else if (menuChoice == 5) { data.showAll(); } else if (menuChoice == 6) { System.out.println("Thank you!"); } else { System.out.println("Error"); return; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
