Files
oop/lab/lab8/Question5/Shape.java
2022-04-17 11:41:41 +08:00

90 lines
1.8 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws CannotResizeException;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}