-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrototypeCodingExercise.java
72 lines (60 loc) · 1.51 KB
/
PrototypeCodingExercise.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Prototype section exercise: Given the class definitions, implement Line.deepCopy() to perform a deep
copy of the current Line object
*/
package creational.prototype;
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
class Line
{
public Point start, end;
public Line(Point start, Point end)
{
this.start = start;
this.end = end;
}
// copy method: create a new line with new points and return the new line
public Line deepCopy ()
{
// First construct the new Line object and then clone it
Line other = new Line(new Point (start.x, start.y), new Point(end.x, end.y));
return other;
}
@Override
public String toString() {
return "Line{" +
"start=" + start +
", end=" + end +
'}';
}
}
class ExerciseDemo {
public static void main(String[] args) {
Line l1 =
new Line (
new Point(100,100),
new Point(200, 200));
Line l2 = l1.deepCopy();
System.out.println(l1);
System.out.println("l2 is a deep copy of l1:");
System.out.println(l2);
System.out.println("Change x coordinate on l2:");
l2.start.x = 1;
System.out.println(l1);
System.out.println(l2);
}
}