← all problems

Exam Free Response 1 Practice: GeoTools.describeRoute 🌶️🌶️

Continuing the GeoTools class from the previous problem. You may assume quadrantLabel works as specified, regardless of what you wrote for it. Write the describeRoute method: given two Point2D parameters, start and end, return a String in the exact format "Distance: d, ending in quadrant Q", where d is the distance from start to end and Q is the quadrant (or "origin"/"axis") that end is in. For example, describeRoute from (0, 0) to (3, 4) should return "Distance: 5.0, ending in quadrant I".

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

GeoTools.java — the class below is already written; you're only completing the method in the editable box.

class GeoTools {
    public static String quadrantLabel(Point2D p) {
        if (p.getX() == 0 && p.getY() == 0) {
            return "origin";
        }
        if (p.getX() == 0 || p.getY() == 0) {
            return "axis";
        }
        if (p.getX() > 0 && p.getY() > 0) {
            return "I";
        }
        if (p.getX() < 0 && p.getY() > 0) {
            return "II";
        }
        if (p.getX() < 0 && p.getY() < 0) {
            return "III";
        }
        return "IV";
    }
    // ---- write your method below ----
}