64 lines
1.5 KiB
Java
64 lines
1.5 KiB
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-04-14
|
|
* Description: Circle class inherit from Shape class
|
|
*/
|
|
|
|
public class Circle extends Shape {
|
|
private double radius;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param x the x coordinate of the center of the circle
|
|
* @param y the y coordinate of the center of the circle
|
|
* @param radius the radius of the circle
|
|
*/
|
|
public Circle(double x, double y, double radius) {
|
|
super(x, y);
|
|
this.radius = radius;
|
|
}
|
|
|
|
/**
|
|
* Calculates the area of the circle.
|
|
*
|
|
* @return the area of the circle
|
|
*/
|
|
@Override
|
|
public double area() {
|
|
return Math.PI * radius * radius;
|
|
}
|
|
|
|
/**
|
|
* Resizing a circle cahnges its radius to be newRadius.
|
|
*
|
|
* @param newRadius
|
|
*/
|
|
@Override
|
|
public void resize(double newRadius) {
|
|
radius = newRadius;
|
|
}
|
|
|
|
/**
|
|
* Test.
|
|
*/
|
|
public static void testCircle() {
|
|
Circle c = new Circle(1.2, 3.4, 4.0);
|
|
// getX, getY, and move are inherited from Shape.
|
|
// area and resize come from Circle itself.
|
|
System.out.println(c.getX() == 1.2);
|
|
System.out.println(c.getY() == 3.4);
|
|
System.out.println(c.area() == Math.PI * 16.0);
|
|
// Move the circle. The area does not change.
|
|
c.move(7.8, 9.0);
|
|
System.out.println(c.getX() == 9.0);
|
|
System.out.println(c.getY() == 12.4);
|
|
System.out.println(c.area() == Math.PI * 16.0);
|
|
// Resize the circle. The area changes but not the position.
|
|
c.resize(8.0);
|
|
System.out.println(c.getX() == 9.0);
|
|
System.out.println(c.getY() == 12.4);
|
|
System.out.println(c.area() == Math.PI * 64.0);
|
|
}
|
|
}
|