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 interface in the Student class. To do this you need to: a.Declare this in the header of the student class: public class Student implements Comparable b.Complete the code of the compareTo method that compares this Student with the other Student (passed as a parameter) based on the credits of the students. i.e. this Student is greater than the other Student if its credits are more than of the other student. c.In the LabClass class define a method called findLargest. This method should return the Student in the students Arraylist that has the largest number of credits. A commented skeleton of the method is given to you in LabClass. Uncomment it and fill in the missing code.

import java.util.*; public class LabClass { private String instructor; private String room; private String timeAndDay; private ArrayList students; private int capacity;

/** * 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(); capacity = maxNumberOfStudents; }

/** * 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

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!