KaiquanMah commited on
Commit
0f2f9b6
·
verified ·
1 Parent(s): 5b9cf26

super(x,y);

Browse files
Week 5: Class hierarchies/05B. Modifiable point ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program defines the class 'Point'.
2
+
3
+ Write the class 'Modifiablepoint', which inherits the class 'Point'.
4
+ The class must have the following properties:
5
+ A constructor that takes x and y as parameters and sets the values of the attributes inherited from the parent class according to them
6
+ Observation methods getX and getY
7
+ The setX and setY setting methods
8
+
9
+
10
+
11
+ import java.util.Random;
12
+
13
+ public class Test{
14
+ public static void main(String[] args){
15
+ final Random r = new Random();
16
+
17
+
18
+ System.out.println("Testing class Modifiablepoint...");
19
+ for (int testi=1; testi<=3; testi++) {
20
+ System.out.println("Test " + testi);
21
+ Modifiablepoint mp = new Modifiablepoint(r.nextInt(20), r.nextInt(20));
22
+ System.out.println("Object created!");
23
+
24
+ System.out.println("Get:");
25
+ System.out.println("x: " + mp.getX());
26
+ System.out.println("y: " + mp.getY());
27
+
28
+ System.out.println("Set: ");
29
+ mp.setX(r.nextInt(20));
30
+ mp.setY(r.nextInt(20));
31
+ System.out.println("x: " + mp.getX());
32
+ System.out.println("y: " + mp.getY());
33
+
34
+ System.out.println("");
35
+ }
36
+ }
37
+ }
38
+
39
+ class Point {
40
+ protected int x;
41
+ protected int y;
42
+
43
+ public Point(int x, int y) {
44
+ this.x = x;
45
+ this.y = y;
46
+ }
47
+ }
48
+
49
+
50
+
51
+
52
+ //ADD
53
+ class Modifiablepoint extends Point {
54
+ public Modifiablepoint(int x, int y) {
55
+ super(x,y);
56
+ }
57
+
58
+ public int getX() {
59
+ return this.x;
60
+ }
61
+
62
+ public int getY() {
63
+ return this.y;
64
+ }
65
+
66
+ public void setX(int x) {
67
+ this.x = x;
68
+ }
69
+
70
+ public void setY(int y) {
71
+ this.y = y;
72
+ }
73
+ }
74
+
75
+
76
+
77
+
78
+
79
+ Testing class Modifiablepoint...
80
+ Test 1
81
+ Object created!
82
+ Get:
83
+ x: 19
84
+ y: 17
85
+ Set:
86
+ x: 13
87
+ y: 15
88
+
89
+ Test 2
90
+ Object created!
91
+ Get:
92
+ x: 17
93
+ y: 3
94
+ Set:
95
+ x: 4
96
+ y: 3
97
+
98
+ Test 3
99
+ Object created!
100
+ Get:
101
+ x: 17
102
+ y: 18
103
+ Set:
104
+ x: 18
105
+ y: 0
106
+
107
+
108
+
109
+