Need for Java (a) A point is inside the circle. (b) A circle is inside another c
ID: 3784962 • Letter: N
Question
Need for Java
(a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another circle.
Define the Circle 2D 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 gerArea that returns the area of the circle A method geteerimeter that returns the perimeter of the circle A method double x, double y) that returns true if the specified point (x, y) is inside this circle. See Figure 10.14(a) A method Circle2D circle) that returns true if the specified circle is inside this circle. See Figure 10.14(b) A method Circle2D circle) that returns true if the specified circle overlaps with this circle. See the figure below (b) (c) Figure (a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another circle.Explanation / Answer
// 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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.