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

73 lines
1.7 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}