Question: Rewrite the Course class in Listing 10.6 to add a clone method to perform a deep copy on the students field. Listing 10.6 - Course.java

Rewrite the Course class in Listing 10.6 to add a clone method to perform a deep copy on the students field.

Listing 10.6 - Course.java public class Course i private String courseNane: private

Listing 10.6 - Course.java public class Course i private String courseNane: private string ) students new string(1001; private int numberofstudents: public Course (String courselame) this.courselane - courseName; public void addstudent (String student) I studenta (numberorstudents) student: numberofstudents+ public Stringti getstudents () return students: public int getlumberorstudents () return numberofstudents; public string getCourselame () zeturn courselane; public void dropstudent (String st udent) II Left as an exercise in Exercise 9.9 The implementation listed above will crash if more than 100 students are added. The addStudent method should be rewriten as folows: public void addstudent (String student) ( 1f (numstudents < 100) I students (numberorstudents) - student: numberofstudents++ else System.out.printin ("Course is ful1"): Returning a reference to the students array is a serious security issue since the dient class is able to make modifications. The getStudent method should be rewritten as follows: public string 1 getstudents () ( Stringtl studentaCopy - new String(numberotstudents): Systen.arraycopy (atudents, 0 , studentsCopy, 0, numberofstudents) : return studentaCopy:

Step by Step Solution

3.57 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Exercise1313 Main method public static void mainString args Course1 course1 new Course1... View full answer

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 Introduction to Java Programming and Data Structure Questions!