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 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
Get step-by-step solutions from verified subject matter experts
