lab8 finished
This commit is contained in:
72
lab/lab8/Question8/Dot.java
Normal file
72
lab/lab8/Question8/Dot.java
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user