Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rewrite the Course class in Listing 10.6 to implement the comparable and the clo

ID: 3676700 • Letter: R

Question

Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable
interfaces. The clone method must allow a deep copy on the students field. In addition, add the
equals(Object o) and the toString() methods.
Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way.

10.5 Case Study: Designing the Course Class 377 Number of students in course1: 3 Peter Jones, Kim Smith, Anne Kennedy, Number of students in course2: 2 The Course class is implemented in Listing 10.6. It uses an array to store the students in the course. For simplicity, assume that the maximum course enrollment is 100, The array is cre- ated using new String[100] in line 3. The addStudent method (line 10) adds a student to the array. Whenever a new student is added to the course, numberOfStudents is increased (line 12). The getStudents method returns the array. The dropStudent method (line 27) is left as an exercise. LISTING 10.6 Course.java 1 public class Course[ 2 private String courseName; 3 private String[] students new String[100]; 4 private int numberOfStudents; create Students 6 public Course(String courseName) this. courseName cours eName ; = 9 10 public void addStudent (String student) students [numberOfstudents] student; numberOfstudents++ 12 13 14 15 public String[] getStudents 1 16 return Students return students; 18 19 public int getNumberOfstudentsO 20 21 number of students return numberOfstudents 23 public String getCourseName O 24 return courseName; 27 public void dropStudent(String student) // Left as an exercise in Programming Exercise 10.9 29 30 The array size is fixed to be 100 line 3), so you cannot have more than 100 students in the course. You can improve the class by automatically increasing the array size in Programming Exercise 10.9 When you create a Course object, an array object is created. A Course object contains a reference to the array. For simplicity, you can say that the Course object contains the array The user can create a Course object and manipulate it through the public methods addStudent, dropStudent, getNumberOfStudents, and getStudents. However, the user doesn't need to know how these methods are implemented. The Course class encapsu- lates the internal implementation. This example uses an array to store students, but you could use a different data structure to store students. The program that uses Course does not need to change as long as the contract of the public methods remains unchanged.

Explanation / Answer

public class main {
public static void main(String[] args) {
    Course course1 = new Course("Data Structures");
    Course course2 = new Course("Database Systems");

    course1.addStudent("Peter Jones");
    course1.addStudent("Brian Smith");
    course1.addStudent("Anne Kennedy");

    course2.addStudent("Peter Jones");
    course2.addStudent("Steve Smith");

    System.out.println("Number of students in course1: " + course1.getNumberOfStudents());
    String[] students = course1.getStudents();
    for (int i = 0; i < course1.getNumberOfStudents(); i++){
      System.out.print(students[i] + ", ");
    }

    System.out.println();
    System.out.println("Number of students in course2: " + course2.getNumberOfStudents());

    course1.dropStudent("Brian Smith");
    System.out.println("Number of students in course1: " + course1.getNumberOfStudents());
    students = course1.getStudents();
  
    for (int i = 0; i < course1.getNumberOfStudents(); i++){
      System.out.print(students[i] + ", ");
    }
}
}

class Course{
   private String courseName;
   private String[] students = new String[100];
   private int numberOfStudents;
  
   public Course(){
   }
  
   public Course(String courseName){
       this.setCourseName(courseName);
   }
  
   public void addStudent(String student){
       students[numberOfStudents] = student;
       numberOfStudents++;
   }
  
   public String[] getStudents(){
       return students;
   }
  
   public int getNumberOfStudents(){
       return numberOfStudents;
   }
  
   public void dropStudent(String student){
       int pos = 0;
       String[] temp = new String[students.length];
       for(int i = 0; i < students.length; i++){
           if(students[i] != student){
               temp[pos] = students[i];
               pos++;
           }
       }
       students = temp;
   }
  
   public void clear(){
       String[] temp = new String[100];
       students = temp;
   }

   public String getCourseName() {
       return courseName;
   }

   public void setCourseName(String courseName) {
       this.courseName = courseName;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote