*8.18 (Geometry: The Circle2D class) Define the Circle2D class that contains Two
ID: 3698719 • Letter: #
Question
*8.18 (Geometry: The Circle2D class) Define the Circle2D class that contains Two private float data fields named x and y that specify the center of the circle with get/set methods A private data field radius with get/set methods. A constructor that creates a circle with the specified x, y, and radius. The default values are all 0 A method getArea() that returns the area of the circle A method getPerimeterO that returns the perimeter of the circle. A method containsPoint (x, y) that returns True if the specified point (x, y) is inside this circle (see Figure 8.10a). A method contains (circle2D) that returns True if the specified circle is inside this circle (see Figure 8.10b) A method overlaps (circle2D) that returns True if the specified circle overlaps with this circle (see Figure 8.10c) Implement the_contains_(another) method that returns True if this circle is contained in another circleExplanation / Answer
Circle2D.java
public class Circle2D {
private double x;
private double y;
private double radius;
public Circle2D() {
this.x = 0;
this.y = 0;
this.radius = 1.0;
}
public Circle2D(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public double getRadius() {
return this.radius;
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
public double getArea() {
return Math.PI * (this.radius * this.radius);
}
public boolean containsPoint(double x, double y) {
double d = Math.sqrt(Math.pow((x - this.x), 2)
+ Math.pow((y - this.y), 2));
if (d < this.radius)
return true;
else
return false;
}
public boolean contains(Circle2D circle) {
double d = Math.sqrt(Math.pow((circle.x - this.x), 2)
+ Math.pow((circle.y - this.y), 2));
if (d + circle.radius < this.radius)
return true;
else
return false;
}
public boolean overlaps(Circle2D circle) {
double d = Math.sqrt(Math.pow((circle.x - this.x), 2)
+ Math.pow((circle.y - this.y), 2));
if (d <= circle.radius + this.radius)
return true;
else
return false;
}
}
________________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double x1,y1,r1,x2,y2,r2;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter x1, y1, radius1:");
String str1=sc.nextLine();
String arr1[]=str1.split(",");
x1=Double.parseDouble(arr1[0]);
y1=Double.parseDouble(arr1[1]);
r1=Double.parseDouble(arr1[2]);
//Getting the input entered by the user
System.out.print("Enter x2, y2, radius2:");
String str2=sc.nextLine();
String arr2[]=str2.split(",");
x2=Double.parseDouble(arr2[0]);
y2=Double.parseDouble(arr2[1]);
r2=Double.parseDouble(arr2[2]);
//Creating an Instance of Circle class
Circle2D c1=new Circle2D(x1, y1, r1);
Circle2D c2=new Circle2D(x2, y2, r2);
//Displaying the area,perimter of circle1 and circle2
System.out.println("Area of c1 is "+c1.getArea());
System.out.println("Perimeter of c1 is "+c1.getPerimeter());
System.out.println("Area of c2 is "+c2.getArea());
System.out.println("Perimeter of c2 is "+c2.getPerimeter());
System.out.println("c1 contains the center of c2 ? "+c1.containsPoint(x2, y2));
System.out.println("c1 contains c2 ? "+c1.contains(c2));
System.out.println("c1 overlaps c2 ? "+c1.overlaps(c2));
}
}
____________________
Output:
Enter x1, y1, radius1:5,5.5,10
Enter x2, y2, radius2:9,1.3,10
Area of c1 is 314.1592653589793
Perimeter of c1 is 62.83185307179586
Area of c2 is 314.1592653589793
Perimeter of c2 is 62.83185307179586
c1 contains the center of c2 ? true
c1 contains c2 ? false
c1 overlaps c2 ? true
________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.