-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise13_13.java
More file actions
59 lines (49 loc) · 1.42 KB
/
Exercise13_13.java
File metadata and controls
59 lines (49 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Exercise13_13 {
public static void main(String[] args) {
Course1 course1 = new Course1("DS");
course1.addStudent("S1");
course1.addStudent("S2");
course1.addStudent("S3");
Course1 course2 = (Course1) course1.clone();
course2.addStudent("S4");
course2.addStudent("S5");
course2.addStudent("S6");
System.out.println(course1.getNumberOfStudents());
System.out.println(course2.getNumberOfStudents());
}
}
class Course1 implements Cloneable {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course1(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourse1Name() {
return courseName;
}
public void dropStudent(String student) {
// Left as an exercise in Exercise 10.9
}
public Object clone() {
try {
Course1 c = (Course1) super.clone();
c.students = new String[100];
System.arraycopy(students, 0, c.students, 0, 100);
c.numberOfStudents = numberOfStudents;
return c;
} catch (CloneNotSupportedException ex) {
return null;
}
}
}