I need help creating a Java program that is basic and simple. Here is the exerci
ID: 3800607 • Letter: I
Question
I need help creating a Java program that is basic and simple. Here is the exercise:
Create class Circle2D with the following specifications:
Circle2D
- x : double
- y : double
- radius : double
+Circle2D( )
+Circle2D(x : double, y : double, radius : double)
+ getX( ) : double
+ getY( ) : double
+ getRadius( ) : double
+ getArea( ) : double
+ getPerimeter( ) : double
- distance(x1 : double, y1 : double, x2 : double, y2 :double) : double
+ contains(x : double, y : double) : boolean
+ contains(circle : Circle2D) : boolean
+ overlaps(circle : Circle2D) : boolean
Two private double data fields named x and y that specify the center of the circle with get methods.
A private 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. Perimeter is circumference.
A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle (see Figure 10.15a).
A method contains(Circle2D circle) that returns true if the specified circle is inside this circle (see Figure 10.15b).
A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle (see Figure 10.15c).
Please see the textbook for figure 10.15, pg. 403.
Write a test program that creates a
Circle2D object c1 (new Circle2D(2, 2, 5.5))
displays its area and perimeter
displays the result of c1.contains(3, 3)
displays the result of c1.contains(new Circle2D(4, 5, 10.5))
displays the result of c1.overlaps(new Circle2D(3, 5, 2.3)).
TIPS FOR 10.11
How to determine if circles C1 and C2 overlap:
Two circles overlap if the distance between the two centers are less than or equal to the sum of their radii.
i.e. distance(circle1 x-coord, circle1 y-coord, circle2 x-coord, circle 2 y=coord) <=
(the radius of Circle 1 + radius of Circle 2)
How to determine if a C1 is contained within C2:
(The distance between the x,y coordinates of C1 and C2 + the radius of C2) is <= radius of C1
How to determine if a point x1,y1 is contained in a circle, C1:
(The distance of the x,y coordinates of C1 from x1, y1) is <= the radius of C1
//Method distance finds the distance between two points.
private static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
Explanation / Answer
//Cirlce2D.java
import java.io.*;
import java.util.*;
class Circle2D
{
private double x;
private double y;
private double radius;
public Circle2D()
{
x=0;
y=0;
radius=1;
}
Circle2D(double x,double y,double radius)
{
this.x=x;
this.y=y;
this.radius=radius;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return Math.PI*radius*radius;
}
public double getPerimeter()
{
return 2*Math.PI*radius;
}
public boolean contains(double x1,double y1)
{
if(Math.abs(distance(x,y,x1,y1))<=radius)
return true;
return false;
}
public boolean contains(Circle2D c1)
{
if((Math.abs(distance(c1.getX(),c1.getY(),x,y))+radius)<=c1.getRadius())
return true;
return false;
}
public boolean overlaps(Circle2D c1)
{
if(Math.abs(distance(c1.getX(),c1.getY(),x,y))<=(c1.getRadius()+radius))
return true;
return false;
}
private static double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
}
//CirlceDriver.java
import java.io.*;
import java.util.*;
public class CircleDriver
{
public static void main(String args[])
{
Circle2D c=new Circle2D(2,2,5.5);
System.out.println("Area of Cirlce is : "+c.getArea());
System.out.println("Perimeter of circle is :"+c.getPerimeter());
if(c.contains(3,3))
System.out.println("Point (3,3) is inside the cirlce");
else
System.out.println("Point (3,3) is not inside the circle");
if(c.contains(new Circle2D(4,5,10.5)))
System.out.println("The given circle is inside the circle");
else
System.out.println("The given circle is not inside the circle");
if(c.overlaps(new Circle2D(3,5,2.3)))
System.out.println("overlaps");
else
System.out.println("Not overlaps");
}
}
//Output :
G580:~/codes/schegg$ javac CircleDriver.java
G580:~/codes/schegg$ java CircleDriver
Area of Cirlce is : 95.03317777109123
Perimeter of circle is :34.55751918948772
Point (3,3) is inside the cirlce
The given circle is inside the circle
overlaps
G580:~/codes/schegg$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.