import java.text.DecimalFormat; // Class representing a student public class Student { private String first; // first name private String last; // last name private double gpa; // grade point average // Student class constructor public Student(String first, String last, double gpa) { this.first = first; // first name this.last = last; // last name this.gpa = gpa; // grade point average } public double getGPA() { return gpa; } public String getLast() { return last; } // Returns a String representation of the Student object, with the GPA formatted to one decimal public String toString() { DecimalFormat fmt = new DecimalFormat("#.0"); return first + " " + last + " " + "(GPA: " + fmt.format(gpa) + ")"; } } public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); Course course = new Course(); int beforeCount; // Example students for testing course.addStudent(new Student("Henry", "Nguyen", 3.5)); course.addStudent(new Student("Brenda", "Stern", 2.0)); course.addStudent(new Student("Lynda", "Robison", 3.2)); course.addStudent(new Student("Sonya", "King", 3.9)); Student student = course.findStudentHighestGpa(); System.out.println("Top student: " + student); } }
public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
Course course = new Course();
int beforeCount;
// Example students for testing
course.addStudent(new Student("Henry", "Nguyen", 3.5));
course.addStudent(new Student("Brenda", "Stern", 2.0));
course.addStudent(new Student("Lynda", "Robison", 3.2));
course.addStudent(new Student("Sonya", "King", 3.9));
Student student = course.findStudentHighestGpa();
System.out.println("Top student: " + student);
}
}
1. Create a class named Student with private fields: first (String), last (String), and gpa (double).
2. Implement a constructor in the Student class to initialize first, last, and gpa.
3. Implement getGPA() method in the Student class to retrieve the GPA of the student.
4. Override toString() method in the Student class to return a formatted string representation of the student.
5. Create a class named Course with an ArrayList of Student objects named roster.
6. Implement a method named findStudentHighestGpa() in the Course class.
7. Initialize a Student object variable (highestGpaStudent) to null to keep track of the student with the highest GPA.
8. Iterate through the roster list:
a. For each student, compare their GPA with the GPA of highestGpaStudent.
b. If the student's GPA is higher, update highestGpaStudent with the current student.
9. After iterating through the roster, highestGpaStudent will contain the student with the highest GPA.
10. Return highestGpaStudent from the findStudentHighestGpa() method.
Step by step
Solved in 4 steps with 4 images