Closed. This question needs to be more
I have done all the setters and getter objects except for the contains and overlaps
This is the test parameters with the expected output
https://i.stack.imgur.com/HJMYG.png
This is the code I have done
public class Question03 {
public static void main(String[] args) {
Circle c1 = new Circle(new Point(0, 0), 1.0);
System.out.println(c1.getArea());
Circle c2 = new Circle(new Point(1, 0), 1.0);
System.out.println(c2.getArea());
System.out.println(c1.getCenter().distance(c2.getCenter()));
System.out.println(c1.contains(new Point(0, 0.5)));
System.out.println(c1.overlaps(c2));
Circle c3 = new Circle(new Point(0, 4), 1.0);
System.out.println(c3.contains(new Point(0, 0)));
System.out.println(c3.overlaps(c1));
public class Circle {
Point center;
double radius;
Circle (Point center, double radius){
this.center=center;
this.radius=radius;
}
public Point getCenter() {
return this.center;
}
public double getRadius(){
return this.radius;
}
public Point setCenter(){
return this.center;
}
public double setRadius(){
return this.radius;
}
public double getArea(){
double area = Math.PI * radius * radius;
return area;
}
public double getPerimeter(){
double perimeter = Math.PI * (radius * 2);
return perimeter;
}
public boolean contains(Point p){
return contains;
}
public boolean overlaps(Circle c){
return overlaps;
}
}
public class Point {
double x;
double y;
Point(double x, double y){
this.x=x;
this.y=y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public double setX(){
return this.x;
}
public double setY(){
return this.y;
}
public double distance(Point p){
double distance = Math.sqrt((-x)*(-x) (-y)*(-y));
return distance;
}
}
I don't know what to do after this I am really stuck and I can't find anything on this on the interent please help :(
CodePudding user response:
Properly apply the distance formula between "this" point and the "p" passed in as a parameter. Next contains is true if the point "p" passed in as a parameters distance is less than the circles radius. Lastly overlaps is true if the distance between the centers is less than the sum of the radius of each circle.
The code is untested but it probably works if everything else in the project works.
public double distance(Point p){
return Math.sqrt( (this.x - p.getX()) * (this.x - p.getX())
(this.y - p.getY()) * (this.y - p.getY())
);
}
public boolean contains(Point p){
return this.center.distance(p) < this.radius;
}
public boolean overlaps(Circle c){
double d = this.center.distance(c.getCenter());
return d < this.radius c.getRadius();
}
CodePudding user response:
The distance formula you want is d=√((x_2-x_1)² (y_2-y_1)²)
. Try this:
public double distance(Point p) {
double xDiff = p.getX() - this.x;
double yDiff = p.getY() - this.y;
double xPow = xDiff * xDiff;
double yPow = yDiff * yDiff;
double distance = Math.sqrt(xPow yPow);
return distance;
}
And here's a shorthand version using the Math class's hypot method.
public double distance(Point p) {
return Math.hypot(p.getX() - this.x, p.getY() - this.y);
}