lab8 finished
This commit is contained in:
81
lab/lab8/Question8/Square.java
Normal file
81
lab/lab8/Question8/Square.java
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-14
|
||||
* Description: Square class inherited from Rectangle class
|
||||
*/
|
||||
|
||||
public class Square extends Rectangle {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param x the x coordinate center of the square
|
||||
* @param y the y coordinate center of the square
|
||||
* @param size the size of the square
|
||||
*/
|
||||
public Square(double x, double y, double size) {
|
||||
super(x, y, size, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the resize(double double) method check width == height.
|
||||
*
|
||||
* @param width the new width of the square
|
||||
* @param length the new length of the square
|
||||
*/
|
||||
@Override
|
||||
public void resize(double width, double length) throws CannotResizeException {
|
||||
if (width != length) {
|
||||
throw new CannotResizeException("Cannot resize a square into a rectangle!");
|
||||
}
|
||||
super.resize(width, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testSquare() {
|
||||
Square s = new Square(1.2, 3.4, 5.0);
|
||||
// getX, getY, and move are inherited from Shape.
|
||||
// area and resize are inherited from Rectangle.
|
||||
System.out.println(s.getX() == 1.2);
|
||||
System.out.println(s.getY() == 3.4);
|
||||
System.out.println(s.area() == 25.0);
|
||||
// Move the square. The area does not change.
|
||||
s.move(7.8, 9.0);
|
||||
System.out.println(s.getX() == 9.0);
|
||||
System.out.println(s.getY() == 12.4);
|
||||
System.out.println(s.area() == 25.0);
|
||||
// Resize the square. The area changes but not the position.
|
||||
s.resize(12.0);
|
||||
System.out.println(s.getX() == 9.0);
|
||||
System.out.println(s.getY() == 12.4);
|
||||
System.out.println(s.area() == 144.0);
|
||||
|
||||
// Resize the square again with different width and length!
|
||||
// Now the square is not square anymore!
|
||||
|
||||
try {
|
||||
s.resize(10.0, 15.0);
|
||||
System.out.println(s.getX() == 9.0); // Unreachable.
|
||||
System.out.println(s.getY() == 12.4);
|
||||
System.out.println(s.area() == 150.0);
|
||||
} catch (CannotResizeException ex) {
|
||||
System.out.println(ex.getMessage() == "Cannot resize a square into a rectangle!");
|
||||
}
|
||||
// The area and position do not change.
|
||||
System.out.println(s.getX() == 9.0);
|
||||
System.out.println(s.getY() == 12.4);
|
||||
System.out.println(s.area() == 144.0);
|
||||
// Resize the square again with equal width and length.
|
||||
// The area changes but not the position.
|
||||
try {
|
||||
s.resize(10.0, 10.0);
|
||||
} catch (CannotResizeException ex) {
|
||||
System.out.println("BUG! This must never happen!");
|
||||
}
|
||||
System.out.println(s.getX() == 9.0);
|
||||
System.out.println(s.getY() == 12.4);
|
||||
System.out.println(s.area() == 100.0);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user