← Unit 3 · all exercises
Saved in this browser only

Distance between two points

Write a Point class with x and y, and a distanceTo method taking another Point and returning the straight-line distance between them. Use Math.sqrt.

class Point { Point(int x, int y); double distanceTo(Point other); }

Point.java
Loading editor…
Results

Examples

  • new Point(0, 0).distanceTo(new Point(3, 4)) → 5
  • new Point(0, 0).distanceTo(new Point(0, 0)) → 0

There are also 3 hidden tests you can't see. Solve the problem, not the examples.