Define the Circle2D class that contains Two double data fiel

Define the Circle2D class that contains: Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. A constructor that creates a circle with the specified x, y, and radius. A method getArea() that returns the area of the circle. A method getPerimeter() that returns the perimeter of the circle. A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle. See Fig. (a) below. A method contains(Cirde2D circle) that returns true if the specified circle is inside this circle. See Fig. (b) below. A method overlaps(Cirde2D circle) that returns true if the specified circle overlaps with this circle. See Fig. (c) below. Implement the class. Write a test program that creates a Cirde2D object c1 (new Cirde2D(2, 2, 5.5)), displays its area and perimeter, and displays the result of c1.contains(3, 3), c1.contains(new Cirde2D(4, 5, 10.5)), and c1.overlaps(new Cirde2D(3, 5, 2.3)).

Solution

// Circle2D.java

public class Circle2D
{
private double x, y;
private double radius;
double getRadius()
{
return radius;
}
Circle2D()
{
x = 0;
y = 0;
radius = 1;
}
Circle2D(double a, double b, double r)
{
x = a;
y = b;
radius = r;
}
double getArea()
{
return 3.14 * radius * radius;
}
double getPerimeter()
{
return 2.0 * 3.14 * radius;
}
boolean contains(double x, double y)
{
double temp = (x - this.x) * (x - this.x) + (y - this.y) * (y - this.y);
if(temp < radius * radius) return true;
else return false;
}
boolean contains(Circle2D circle)
{
double temp = (circle.x - x) * (circle.x - x) + (circle.y - y) * (circle.y - y);
if(temp < radius * radius) return true;
else return false;
}
boolean overlaps(Circle2D circle)
{
double temp = (circle.x - x) * (circle.x - x) + (circle.y - y) * (circle.y - y);
if(temp <= radius + circle.radius && temp >= Math.abs(radius - circle.radius)) return true;
else return false;
}
}

// Circle2DTest.java

import java.util.Scanner;
public class Circle2DTest
{
public static void main(String args[])

{
double a1, b1, r1, p, q, a2, b2, r2;
Scanner in = new Scanner(System.in);
System.out.print(\"Enter the centre coordinates of the first circle: \");
a1 = in.nextDouble();
b1 = in.nextDouble();
System.out.print(\"Enter the radius of the first circle: \");
r1 = in.nextDouble();
Circle2D circle1 = new Circle2D(a1, b1, r1);
System.out.println(\"Area = \" + circle1.getArea());
System.out.println(\"Perimeter = \" + circle1.getPerimeter());
System.out.print(\"\ Enter the coordinates of a point: \");
p = in.nextDouble();
q = in.nextDouble();
if(circle1.contains(p, q)) System.out.println(\"The point is inside the Circle\");
else System.out.println(\"The point is outside the Circle\");

System.out.print(\"\ Enter the centre coordinates of the second circle: \");
a2 = in.nextDouble();
b2 = in.nextDouble();
System.out.print(\"Enter the radius of the second circle: \");
r2 = in.nextDouble();
Circle2D circle2 = new Circle2D(a2, b2, r2);
if(circle1.contains(circle2)) System.out.println(\"The second circle is inside the first circle\");
else System.out.println(\"The second circle is outside the first circle\");
if(circle1.overlaps(circle2)) System.out.println(\"\ The second overlaps the first circle\");
else System.out.println(\"\ The second circle does not overlap the first circle\");
}
}


/*
output:

Enter the centre coordinates of the first circle: -5 6
Enter the radius of the first circle: 8
Area = 200.96
Perimeter = 50.24

Enter the coordinates of a point: 4 5
The point is outside the Circle

Enter the centre coordinates of the second circle: 0 1
Enter the radius of the second circle: 9
The second circle is inside the first circle

The second circle does not overlap the first circle

*/

 Define the Circle2D class that contains: Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with
 Define the Circle2D class that contains: Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site