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: 3677026 • 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.

Below is the Listing from the book that needs to be rewritten

public class Course {
   private String courseName;
   private String[] students = new String[100];
   private int numberOfStudents;

   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) {
   }

   public String getCourseName() {
       return courseName;
   }
}

Explanation / Answer

import java.util.Arrays;

public class Course implements Cloneable, Comparable<Course>{
   private String courseName;
   private String[] students = new String[100];
   private int numberOfStudents;

   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) {
   }

   public String getCourseName() {
       return courseName;
   }

   public void setCourseName(String course){
       courseName = course;
   }

   @Override
   public int compareTo(Course o) {
       // comparing based on course name
       return this.courseName.compareToIgnoreCase(o.courseName);
   }
   @Override
   public Course clone() {
       Course p;
       p = new Course(courseName);
       p.students = Arrays.copyOf(students, students.length);
       p.numberOfStudents = numberOfStudents;
       return p;
   }
  
   @Override
   public boolean equals(Object obj) {
       Course c = (Course)obj;
       return courseName.equalsIgnoreCase(c.courseName);
   }
  
   @Override
   public String toString() {
       return "Course[ Name: "+courseName+" Students Number: "+numberOfStudents+" "+studentsName()+" ]";
   }
  
   private String studentsName(){
       StringBuffer bf = new StringBuffer();
       for(int i=0; i<numberOfStudents; i++){
           bf.append(students[i]+" ");
       }
       return bf.toString();
   }
}

// Test class

public class CourseTest {

   public static void doSelectionSort(Course[] arr){

       for (int i = 0; i < arr.length - 1; i++)
       {
           int index = i;
           for (int j = i + 1; j < arr.length; j++)
               if (arr[j].compareTo(arr[index]) < 0)
                   index = j;

           Course smaller = arr[index];
           arr[index] = arr[i];
           arr[i] = smaller;
       }
   }
   public static void main(String[] args) {
       Course c1 = new Course("Computer");

       // adding students
       c1.addStudent("Pinki");
       c1.addStudent("Mukesh");
       c1.addStudent("Alex");
       c1.addStudent("Flex");
       c1.addStudent("Bob");
       // calling toString method
       System.out.println(c1.toString());
       System.out.println();
       // cloning course c1
       Course clone = c1.clone();
       // calling to string on clone
       System.out.println(clone.toString());

       System.out.println();

       // comparing clone and c1

       System.out.println("Is c1 and clone equal ? : "+clone.equals(c1));
       System.out.println();
       // creating course array
       Course[] courses = {
               c1, clone, new Course("Electrical"), new Course("Automation"), new Course("Programming")};

       // printing before sorting
       for(Course c : courses){
           System.out.print(c.getCourseName()+" ");
       }
       System.out.println(" ");
       // sorting c1 , using compare method
       doSelectionSort(courses);

       // printing after sorting
       for(Course c : courses){
           System.out.print(c.getCourseName()+" ");
       }
   }
}

/*

Output:

Course[ Name: Computer
Students Number: 5
Pinki Mukesh Alex Flex Bob
]

Course[ Name: Computer
Students Number: 5
Pinki Mukesh Alex Flex Bob
]

Is c1 and clone equal ? : true

Computer Computer Electrical Automation Programming

Automation Computer Computer Electrical Programming

*/

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