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.
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.