Question: How can I sort students by their GPA and level? This is the code I have so far: public class Student { private String name;

How can I sort students by their GPA and level? This is the code I have so far:

public class Student { private String name; private int id; private double gpa; private String level; public Student(String name, int id, double gpa, String level) { this.name = name; this.id = id; this.gpa = gpa; this.level = level; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getID() { return id; } public void setID(int id) { this.id = id; } public double getGPA() { return gpa; } public void setGPA(double gpa) { this.gpa = gpa; } public String getLevel(){ return level; } public void setLeve(String level){ this.level = level; } public String toString() { //toString is a String representation of a Student object formatted how you want String x = "Name: " + name + ", ID: " + id + ", GPA: " + gpa +",Level:" + level; return x; } }

------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner; import java.util.ArrayList; import java.io.*; public class StudentDriver { //list to hold the Student objects private static ArrayList students = new ArrayList(); private static ArrayListfiles = new ArrayList(); //to get input from user private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { while (true) { int option = displayMenu(); if (option == 1) { createStudent(); } else if (option == 2) { displayAllStudents(); } else if (option == 3) { deleteStudent(); } else if (option == 4) { modifyStudent(); } else if (option == 5) { saveStudents(); } else if (option == 6) { System.out.println("Goodbye!"); System.exit(0); } } } public static void saveStudents() { System.out.print("Enter file name: "); String fileName = sc.next(); try { PrintWriter pw = new PrintWriter(fileName); for (Student s : students) pw.println(s.getName() + "," + s.getID() + "," + s.getGPA()); pw.close(); System.out.println("Student data saved to file successfully"); } catch (FileNotFoundException ex) { System.out.println("File not found"); } catch (IOException ex) { System.out.println("File could not be written to"); } } public static void modifyStudent() { //ask for student ID to modify that student with that ID System.out.print("Enter ID of student you want to modify: "); int id = sc.nextInt(); boolean studentExists = doesStudentExist(id); if (studentExists == false) { System.out.println("Student with that ID not found"); } else { System.out.print("Do you want to update name or GPA for this student? "); String nameOrGPA = sc.next().toUpperCase(); while(!(nameOrGPA.equals("NAME") || nameOrGPA.equals("GPA"))) { System.out.println("Invalid option."); System.out.print("Do you want to update name or GPA for this student? "); nameOrGPA = sc.next().toUpperCase(); } Student s = getStudent(id); if (nameOrGPA.equals("NAME")) { System.out.print("Enter name: "); String name = sc.next(); s.setName(name); System.out.println("Student's name modified successfully"); } else { System.out.print("Enter GPA: "); double gpa = sc.nextDouble(); s.setGPA(gpa); System.out.println("Student's GPA modified successfully"); } } } public static Student getStudent(int id) { Student theStudent = null; for (int i=0; i

public static void createStudent() { //ask for student info System.out.print("Enter name: "); String name = sc.next(); System.out.print("Enter ID: "); int id = sc.nextInt(); System.out.print("Enter GPA: "); double gpa = sc.nextDouble(); System.out.print("Enter level: "); String level = sc.next(); boolean studentExists = doesStudentExist(id); if (studentExists == false) { //create Student object from this data Student s = new Student(name, id, gpa, level); //add this student to the list students.add(s); System.out.println("Student created successfully"); } else { System.out.println("Student with that ID already exists"); } } public static int displayMenu() { System.out.println(" 1. Create a new student"); System.out.println("2. Display all students"); System.out.println("3. Delete student"); System.out.println("4. Modify student"); System.out.println("5. Save student"); // System.out.println("6. Display files:"); // System.out.println("2. Display all students"); System.out.println("6. Exit"); int option = sc.nextInt(); if (option < 1 || option > 7) { System.out.println("Invalid option"); displayMenu(); } System.out.println(); return option; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!