Spaces:
Running
Running
File size: 1,949 Bytes
0f2f9b6 |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
The program defines the class 'Point'.
Write the class 'Modifiablepoint', which inherits the class 'Point'.
The class must have the following properties:
A constructor that takes x and y as parameters and sets the values of the attributes inherited from the parent class according to them
Observation methods getX and getY
The setX and setY setting methods
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
System.out.println("Testing class Modifiablepoint...");
for (int testi=1; testi<=3; testi++) {
System.out.println("Test " + testi);
Modifiablepoint mp = new Modifiablepoint(r.nextInt(20), r.nextInt(20));
System.out.println("Object created!");
System.out.println("Get:");
System.out.println("x: " + mp.getX());
System.out.println("y: " + mp.getY());
System.out.println("Set: ");
mp.setX(r.nextInt(20));
mp.setY(r.nextInt(20));
System.out.println("x: " + mp.getX());
System.out.println("y: " + mp.getY());
System.out.println("");
}
}
}
class Point {
protected int x;
protected int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
//ADD
class Modifiablepoint extends Point {
public Modifiablepoint(int x, int y) {
super(x,y);
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
Testing class Modifiablepoint...
Test 1
Object created!
Get:
x: 19
y: 17
Set:
x: 13
y: 15
Test 2
Object created!
Get:
x: 17
y: 3
Set:
x: 4
y: 3
Test 3
Object created!
Get:
x: 17
y: 18
Set:
x: 18
y: 0
|