B
byhesed
What's wrong with contains and overlaps method in Circle2D class?
It returns incorrect answers.
- A method contains(double x, double y) returns true if the specified
point(x,y) is inside this circle.
- A method contains(Circle2D circle) returns true if the specified
circle is inside this circle.
- A method overlaps(Circle2D circle) return true if the specified
circle overlaps with this circle.
I calculate the distance between two circles:
- double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
It's wired too me because I think it is not wrong.
// TEST CODE
public class Exercise10_11 {
public static void main(String[] args) {
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println("Area is " + c1.getArea());
System.out.println("Perimeter is " + c1.getPerimeter());
System.out.println(c1.contains(3, 3));
System.out.println(c1.contains(new Circle2D(4, 5, 10.5)));
System.out.println(c1.overlaps(new Circle2D(3, 5, 2.3)));
}
}
// Circle2D Class
public class Circle2D {
private double x;
private double y;
private double radius;
public Circle2D() {
x = 0;
y = 0;
radius = 1;
}
public 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 getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public boolean contains(double x, double y) {
double distance = Math.pow(getX() - x, 2.0) + Math.pow(getY() - y,
2.0);
if (distance <= radius)
return true;
else
return false;
}
public boolean contains(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
if (distance <= Math.abs(radius - circle.radius))
return true;
else
return false;
}
public boolean overlaps(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
if (distance > Math.abs(radius - circle.radius) && distance <
Math.abs(radius + circle.radius))
return true;
else
return false;
}
}
It returns incorrect answers.
- A method contains(double x, double y) returns true if the specified
point(x,y) is inside this circle.
- A method contains(Circle2D circle) returns true if the specified
circle is inside this circle.
- A method overlaps(Circle2D circle) return true if the specified
circle overlaps with this circle.
I calculate the distance between two circles:
- double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
It's wired too me because I think it is not wrong.
// TEST CODE
public class Exercise10_11 {
public static void main(String[] args) {
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println("Area is " + c1.getArea());
System.out.println("Perimeter is " + c1.getPerimeter());
System.out.println(c1.contains(3, 3));
System.out.println(c1.contains(new Circle2D(4, 5, 10.5)));
System.out.println(c1.overlaps(new Circle2D(3, 5, 2.3)));
}
}
// Circle2D Class
public class Circle2D {
private double x;
private double y;
private double radius;
public Circle2D() {
x = 0;
y = 0;
radius = 1;
}
public 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 getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public boolean contains(double x, double y) {
double distance = Math.pow(getX() - x, 2.0) + Math.pow(getY() - y,
2.0);
if (distance <= radius)
return true;
else
return false;
}
public boolean contains(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
if (distance <= Math.abs(radius - circle.radius))
return true;
else
return false;
}
public boolean overlaps(Circle2D circle) {
double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
Math.pow(circle.y - getY(), 2.0));
if (distance > Math.abs(radius - circle.radius) && distance <
Math.abs(radius + circle.radius))
return true;
else
return false;
}
}