Consider the Point class below Add a method to the class nam
Consider the Point class below. Add a method to the class named distanceToPoint. The method should accept a Point object as a parameter. It should return the distance between the point that receives the invocation, and the parameter.
Feel free to use Math.sqrt, and Math.pow.
public class Point {
private double x;
private double y;
public void setX(int value) { x = value; }
public double getX() { return x;}
public void setY(int value) { y = value; }
public double getY() { return y;}
// write your distanceTo method as if it is here
}
Solution
Hi, Please find my code:
Please let me know in case of any issue:
public class Point {
private double x;
private double y;
public void setX(int value) { x = value; }
public double getX() { return x;}
public void setY(int value) { y = value; }
public double getY() { return y;}
// write your distanceTo method as if it is here
public double distanceToPoint(Point other){
// distancd: sqrt((x1-x2)^2 + (y1-y2)^2)
double distance = Math.sqrt( Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2) );
return distance;
}
}
