Question: Java. 1.Implement the Comparable interface in the Student class. To do this you need to: a.Declare this in the header of the student class: public
Java. 1.Implement the Comparable
import java.util.*; public class LabClass { private String instructor; private String room; private String timeAndDay; private ArrayList
/** * Create a LabClass with a maximum number of enrolments. All other details * are set to default values. */ public LabClass(int maxNumberOfStudents) { instructor = "unknown"; room = "unknown"; timeAndDay = "unknown"; students = new ArrayList
/** * Add a student to this LabClass. */ public void enrollStudent(Student newStudent) { if(students.size() == capacity) { System.out.println("The class is full, you cannot enroll."); } else { students.add(newStudent); } }
/** * Return the number of students currently enrolled in this LabClass. */ public int numberOfStudents() { return students.size(); }
/** * Set the room number for this LabClass. */ public void setRoom(String roomNumber) { room = roomNumber; }
/** * Set the time for this LabClass. The parameter should define the day * and the time of day. */ public void setTime(String timeAndDayString) { timeAndDay = timeAndDayString; }
/** * Set the name of the instructor for this LabClass. */ public void setInstructor(String instructorName) { instructor = instructorName; }
/** * Print out a class list with other LabClass details to the standard * terminal. */ public String toString() { String time = "Lab class " + timeAndDay; String instr = "Instructor: " + instructor + " room: " + room; String sz = "Number of students: " + numberOfStudents(); return time + " " + instr + " " + sz + " " + "Class list:" + " " + students.toString();
}
/** * Return the student with the highest number of credits. */ // public Student findLargest() // { // // If there are no students return null // // else // Student largestStudentSoFar = students.get(0); // Student currentStudent; // int i = 1; // while (i < ......) // { // currentStudent = students.get(i); // // if the currentStudent is greater than largestStudentSoFar -- call compareTo // // change the value of largestStudentSoFar to be currentStudent // i++; // }
// return largestStudentSoFar;
// } }
public class MyClass { public static void main(String[] args) { LabClass lab = new LabClass (5);
Student s1 = new Student("Danny Drake", "DD1234567"); Student s2 = new Student("Cindy Douglas", "CD234568"); Student s3 = new Student("Opal Schultz", "OS236754"); Student s4 = new Student("Celia Bailey", "CB1234567"); Student s5 = new Student("Ruby Blair", "RB3456712"); Student s6 = new Student("Johanna Sims", "JS3456712");
s1.addCredits(13); s2.addCredits(15); s3.addCredits(19); s4.addCredits(11);
lab.enrollStudent(s1); lab.enrollStudent(s2); lab.enrollStudent(s3);
System.out.println("*************" + " " + lab);
lab.setRoom("Room 267"); lab.setTime("Mon-Fri 8am"); lab.setInstructor("Professor Dave");
System.out.println("Attempting to enroll student: " + s4); lab.enrollStudent(s4); System.out.println("Attempting to enroll student: " + s5); lab.enrollStudent(s5); System.out.println("Attempting to enroll student: " + s6); lab.enrollStudent(s6); System.out.println("*************" + " " + lab); System.out.println("The student with the most credits is " + lab.findLargest());
} }
public class Student { // the student's full name public String name; // the student ID private String id; // the amount of credits for study taken so far private int credits;
/** * Create a new student with a given name and ID number. */ public Student(String fullName, String studentID) { name = fullName; id = studentID; credits = 0; }
/** * Return the full name of this student. */ public String getName() { return name; }
/** * Set a new name for this student. */ public void changeName(String replacementName) { name = replacementName; }
/** * Return the student ID of this student. */ public String getStudentID() { return id; }
/** * Add some credit points to the student's accumulated credits. */ public void addCredits(int additionalPoints) { credits += additionalPoints; }
/** * Return the number of credit points this student has accumulated. */ public int getCredits() { return credits; }
/** * Return the login name of this student. The login name is a combination * of the first four characters of the student's name and the first three * characters of the student's ID number. */ public String getLoginName() { return name.substring(0,4) + id.substring(0,3); } /** * The student's name and ID number. */ public String toString () { return name + " (" + id + ")"; } public int compareTo (Student other) { return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
