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

60 lines
1.3 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 c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.area() == Math.PI * 9.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.resize(4.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.move(5.0, 6.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 6.0);
System.out.println(c1.getY() == 8.0);
}
}