146k views
1 vote
Given LabProgram.java, complete the program by writing the generic classes Course and Student.

The main program reads information of five courses and five students as input and stores the information into an ArrayList of Courses and an ArrayList of Students. The program then sorts both ArrayLists and outputs the sorted information.
Course has private fields:
String department
• Integer number
Course has a constructor and member methods:
• Course(dept, num) - set department to parameter dept and number to parameter num
compareTo(otherCourse) - to enable sorting of Course objects, return -1, 0, or 1 according to the comparisons of the private members between Course and otherCourse. Precedence of comparisons: department (lowest first), then number (lowest first)
• toString() return a string representation of a course in the format "department number"
Student has private fields:
String firstName;
• String lastName;
double GPA;
Student has a constructor and member methods:
• Student(first, last, gradeAverage) - set firstName to parameter first, lastName to parameter last, and GPA to parameter grade Average
compareTo(otherStudent)- to enable sorting of Student objects, return -1, 0, or 1 according to the comparisons of the private
members between Student and otherStudent. Precedence of comparisons: GPA (highest first), then lastName (lowest first), then firstName (lowest first)
toString() - return a string representation of a student in the format "GPA lastName, firstName"
Ex: If the input is:
Chemistry 250
Chemistry 300
Chemistry 200
Biology 200
Biology 100

1 Answer

2 votes

Final answer:

You must define generic classes 'Course' and 'Student' with certain private fields, constructors, comparison logic for sorting, and toString methods for string representations.

Step-by-step explanation:

To complete the LabProgram.java, you need to define two generic classes: Course and Student. The Course class will have private fields for department and number, as well as a constructor, a compareTo method for sorting, and a toString method that returns the course's string representation. Likewise, the Student class will have private fields for firstName, lastName, and GPA, plus similar methods tailored to sorting students based on GPA (descending), lastName, and firstName (both ascending).

Here's an example of what those classes could look like:

// Within Course class
private String department;
private Integer number;
// Constructor and methods...

// Within Student class
private String firstName;
private String lastName;
private double GPA;
// Constructor and methods...

After filling ArrayLists with courses and students and sorting them, you'll output the sorted lists, displaying the information in the specified format.

User Harunurhan
by
8.0k points