Add lab6
This commit is contained in:
51
lab/lab6/Question3/Circle.java
Normal file
51
lab/lab6/Question3/Circle.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) Date: 2022/03/27
|
||||
* Description: This is the class of Circle.
|
||||
*/
|
||||
|
||||
public class Circle extends Shape {
|
||||
private double radius;
|
||||
|
||||
/**
|
||||
* Constructor of Circle.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the area of the circle.
|
||||
*
|
||||
* @return the area of the circle
|
||||
*/
|
||||
@Override
|
||||
public double area() {
|
||||
return Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test
|
||||
*/
|
||||
public static void testCircle() {
|
||||
Circle c1 = new Circle(1.0, 2.0, 3.0);
|
||||
System.out.println(c1.getX() == 1.0);
|
||||
System.out.println(c1.getY() == 2.0);
|
||||
System.out.println(compare(c1.area(), 28.274333882308138));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two double numbers.
|
||||
*
|
||||
* @param a
|
||||
* @param b
|
||||
* @return true if a and b are equal, false otherwise
|
||||
*/
|
||||
public static boolean compare(double a, double b) {
|
||||
return Math.abs(a - b) < 0.000001;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user