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

50 lines
1.4 KiB
Java

/*
* 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);
}
/**
* 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!
// The area changes but not the position.
s.resize(10.0, 15.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 150.0);
}
}