437,911 views
26 votes
26 votes
Add the following method to the Point class: public double distance(Point other) Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y-coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of (x2 - x1)2 (y2 - y1)2. Two points with the same (x, y) coordinates should return a distance of 0.0.

public class Point {
int x;
int y;
// your code goes here
}

User Patroclus
by
2.9k points

1 Answer

9 votes
9 votes

Answer:

public class Point

{

public int x;

public int y;

Point(int x,int y)

{

this.x=x;

this.y=y;

}

public static void main(String[] args)

{

Point p1=new Point(-2,3);

Point p2=new Point(3,-4);

System.out.println("distance:"+distance(p1,p2));

}

private static double distance(Point p1,Point p2)

{

return Math.sqrt(Math.pow(p2.x-p1.x, 2)+Math.pow(p2.y-p1.y, 2));

}

}

Step-by-step explanation:

The java program defines the Point class and the public method 'distance' to return the total distance between the two quadrants passed to it as arguments (they are both instances of the Point class).