KaiquanMah's picture
super(x,y);
0f2f9b6 verified
raw
history blame
1.95 kB
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