In: Computer Science
Define the circle2d class that contains:
Two double data fields named x and y that specify the center of the circle with getter methods
A data field radius with a getter method
A no arg constructor that creates a default circle with 0,0 for x,y and 1 for the radius
A constructor that creates a circle with the specified x,y of the the circle
A method getArea() that returns the area of the circle
A method Contains(Double x, Double y) that returns true if the specified point (x,y) is inside this circle
A mothd Contains(circle2D circle) that returns true if the specified circle is sinside this circle
A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle.
Partial code for visualization
public static void main(String[] args) {
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println("Area is " + c1.getArea());
System.out.println("Perimeter is " + c1.getPerimeter());
System.out.println("c1 contains point (3, 3)? "
+ c1.contains(3, 3));
System.out.println("c1 contains circle Circle2D(4, 5, 10.5)? "
+ c1.contains(new Circle2D(4, 5, 10.5)));
System.out.println("c1 overlaps circle Circle2D(3, 5, 2.3)? "
+ c1.overlaps(new Circle2D(3, 5, 2.3)));
}
Java language
public class Circle2D { private double x, y; private double radius; public Circle2D() { this(0, 0, 1); } public Circle2D(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2 * Math.PI * radius; } private double distance(double x, double y) { return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2)); } public boolean contains(double x, double y) { return distance(x, y) <= radius; } public boolean contains(Circle2D circle) { return distance(circle.x, circle.y) + circle.radius <= this.radius; } public boolean overlaps(Circle2D circle) { return distance(circle.x, circle.y) <= (this.radius + circle.radius); } }