Hi i'm new to java, please help me out !!!
Here topic require:
- display the result of c1.contain(3,3)
- c1.contains(new Circle2D(4, 5, 8.5))
- c1.overlaps(new Circle2D(3, 5, 0.3))
Here is my question:
the circle contain must have (x,y and radius)
why contain(3,3) still work when it doesn't have radius ??
Is my code correct ??
this is my code for circle2D:
public class Circle2D {
// The center of the circle
private double x;
private double y;
// The radius
private double radius;
// Constructor without parameters
public Circle2D() {
this(0, 0, 3);
}
// Constructor with x, y and radius
public Circle2D(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
// Get area method
public double getArea() {
return Math.PI * radius * radius;
}
// Get perimeter
public double getPerimeter() {
return 2 * Math.PI * radius;
}
// Check if the point(X, Y) is within the circle
public boolean contains(double X, double Y) {
if (Math.sqrt((x - X) * (x - X) + (y - Y) * (y - Y)) < radius) {
return true;
} else {
return false;
}
}
// Check if the new circle is with the circle
public boolean contains(Circle2D circle) {
if (Math.sqrt((x - circle.x) * (x - circle.x) + (y - circle.y) * (y - circle.y)) + circle.radius < radius) {
return true;
} else {
return false;
}
}
// Check if the new circle overlaps the circle
public boolean overlaps(Circle2D circle) {
if (Math.sqrt((x - circle.x) * (x - circle.x) + (y - circle.y) * (y - circle.y)) + circle.radius > radius
&& Math.sqrt((x - circle.x) * (x - circle.x) + (y - circle.y) * (y - circle.y)) + circle.radius < radius + circle.radius) {
return true;
} else {
return false;
}
}
public static void main(String[]args){
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println(c1.contains(3,3));
}
}