Problem Description and Specifications Write a Circle class with the following s
ID: 3764423 • Letter: P
Question
Problem Description and Specifications
Write a Circle class with the following specifications
Three double data fields centerX, centerY and radius (centerX and centerY represents the x and y coordinates of the center of the circle)
No-arg constructor which sets the value of zero to centerX and centerY and one to radius.
A constructor with three arguments and set the values for centerX, centerY and radius with the specified values.
The get methods for the three data fields.
A method calculateArea that returns the area of the circle
A method calculatePerimeter 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 Figure 1).
A method contains(Circle c) that returns true if the specified circle c is inside this circle (see Figure 2).
A method overlaps(Circle c) that returns true if the specified circle c overlaps with this circle (see Figure 3).
A toString method which returns a string with circle information.
A method distance(double x1, double y1,double x2, double y2) that returns the distance between the points (x1, y1) and (x2, y2)
W
Figure 1 Figure 2 Figure 3
Note:
A circle contains a point (x, y) if the distance between the point and the center of the circle is less that the radius of the circle.
A circle with radius r1 contains another circle with radius r2 if the distance between their centers is less than r1 – r2
If the distance between the centers of two circle is more than the sum of their radii, they do not overlap.
The distance between two points (x1, y1) and (x2, y2) is given by the formula
Write a TestCircle class that will create
Circle c1 with center at (0,0) and radius 1 and print out the info of circle c1.
Circle c2 with center at (1.5, 0) and radius 3 and print out the info of circle c2.
Circle c3 with center at (2, 2.5) and radius 3.5 and print out the info of circle c3.
Circle c4 with center (3, 3) and radius 1 and print out the info of circle c4.
Determine whether c2 contains the point (2,1.5)
Determine whether c1 contains the point (1.7, 2.3)
Determine whether circle c2 contains circle c1.
Determine whether circle c3 contains circle c2.
Determine whether circle c1 and circle c2 overlap.
Determine whether circle c1 and circle c4 overlap.
Explanation / Answer
Circle.java
/**
* @author srinu
*
*/
public class Circle {
/**
*
*/
double centerX, centerY, radius;
/**
* @param centerX
* the centerX to set
*/
public void setCenterX(double centerX) {
this.centerX = centerX;
}
/**
* @param centerY
* the centerY to set
*/
public void setCenterY(double centerY) {
this.centerY = centerY;
}
/**
* @param radius
* the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* @return
*/
public double calculateArea() {
double area = Math.PI * (radius * radius);
return area;
}
/**
* @return
*/
public double calculatePerimeter() {
double perimeter = 2 * 3.14 * radius;
return perimeter;
}
/**
* @param x
* @param y
* @return
*/
public boolean contains(double x, double y) {
double dist = Math.sqrt((x - centerX) * (x - centerX) - (y - centerY)
* (y - centerY));
if (dist > radius) {
return false;
} else {
return true;
}
}
/**
* @param c
* @return
*/
public boolean contains(Circle c) {
double dist = Math.sqrt((c.centerX - centerX) * (c.centerX - centerX)
- (c.centerY - centerY) * (c.centerY - centerY));
if (dist > radius) {
return false;
} else {
return true;
}
}
/**
* @param c
* @return
*/
public boolean overlaps(Circle c) {
double distance = Math.sqrt(Math.pow(c.centerX - centerX, 2.0)
+ Math.pow(c.centerY - centerY, 2.0));
if (distance > Math.abs(radius - c.radius)
&& distance < Math.abs(radius + c.radius))
return true;
else
return false;
}
/**
* @param x1
* @param y1
* @param x2
* @param y2
* @return
*/
public double distance(double x1, double y1, double x2, double y2) {
double dist = Math.sqrt((x1 - x2) * (x1 - x2) - (y1 - y2) * (y1 - y2));
return dist;
}
/**
* @param centerX
* @param centerY
* @param radius
*/
public Circle(double centerX, double centerY, double radius) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
}
public Circle() {
this.centerX = 0;
this.centerY = 0;
this.radius = 1;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Circle [centerX=" + centerX + ", centerY=" + centerY
+ ", radius=" + radius + "]";
}
}
TestCircle.java
/**
* @author srinu
*
*/
public class TestCircle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Circle c1 with center at (0,0) and radius 1 and print out the info of
// circle c1.
Circle c1 = new Circle();
System.out.println("Circle c1 :" + c1);
// Circle c2 with center at (1.5, 0) and radius 3 and print out the info
// of circle c2.
Circle c2 = new Circle(1.5, 0, 3);
System.out.println("Circle c2 :" + c2);
// Circle c3 with center at (2, 2.5) and radius 3.5 and print out the
// info of circle c3.
Circle c3 = new Circle(2, 2.5, 3.5);
System.out.println("Circle c3 :" + c3);
// Circle c4 with center (3, 3) and radius 1 and print out the info of
// circle c4.
Circle c4 = new Circle(3, 3, 1);
System.out.println("Circle c4 :" + c4);
System.out.println("whether c2 contains the point (2,1.5):"
+ c2.contains(2, 1.5));
// Determine whether c1 contains the point (1.7, 2.3)
System.out.println("whether c1 contains the point (1.7, 2.3):"
+ c1.contains(1.7, 2.3));
// Determine whether circle c2 contains circle c1.
System.out.println("whether circle c2 contains circle c1 :"
+ c2.contains(c1));
// Determine whether circle c3 contains circle c2.
System.out.println("whether circle c3 contains circle c2 :"
+ c3.contains(c2));
// Determine whether circle c1 and circle c2 overlap.
System.out.println("whether circle c1 and circle c2 overlap :"
+ c1.overlaps(c2));
// Determine whether circle c1 and circle c4 overlap.
System.out.println("whether circle c1 and circle c2 overlap :"
+ c1.overlaps(c4));
}
}
OUTPUT:
Circle c1 :Circle [centerX=0.0, centerY=0.0, radius=1.0]
Circle c2 :Circle [centerX=1.5, centerY=0.0, radius=3.0]
Circle c3 :Circle [centerX=2.0, centerY=2.5, radius=3.5]
Circle c4 :Circle [centerX=3.0, centerY=3.0, radius=1.0]
whether c2 contains the point (2,1.5):true
whether c1 contains the point (1.7, 2.3):true
whether circle c2 contains circle c1 :true
whether circle c3 contains circle c2 :true
whether circle c1 and circle c2 overlap :false
whether circle c1 and circle c2 overlap :false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.