← all problems

distanceFromOrigin 🌶️

Write a method that takes one Point2D parameter, p, and returns its distance from the origin, (0, 0). Check the Point2D quick reference if you need a refresher on what methods are available.

Reference: Point2D — the methods you can call

class Point2D

A point on an ordinary (x, y) coordinate plane, with double coordinates. A Point2D never changes once it is made: translate gives you a new one. You do not write this class; make a point with new Point2D(x, y) and call its methods.

Point2D a = new Point2D(0, 0);
Point2D b = new Point2D(3, 4);
a.distanceTo(b)                  // 5.0
a.equals(b)                      // false
a.translate(1, 1).toString()     // "(1.0, 1.0)"
b.getX()                         // 3.0
MethodWhat it does
Point2D(double x, double y)

Makes a point.

  • x – the x-coordinate
  • y – the y-coordinate
double getX()

The x-coordinate.

Returns the x-coordinate

double getY()

The y-coordinate.

Returns the y-coordinate

double distanceTo(Point2D other)

The straight-line distance from this point to another.

  • other – the other point

Returns the distance, as a double

Point2D translate(double dx, double dy)

A NEW point shifted by dx and dy. The point you call it on does not change.

  • dx – how far to move in x (negative moves left)
  • dy – how far to move in y (negative moves down)

Returns the shifted point

boolean equals(Object o)

Whether another object is a Point2D with the same x and y.

  • o – the object to compare with

Returns true if it is a Point2D at the same coordinates; false otherwise

String toString()

The point as text, "(x, y)". It is called for you by System.out.println(p) and by + with a String.

Returns the point as text