Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following method to the Point class: public int manhattanDistance(Point

ID: 3556187 • Letter: A

Question

Add the following method to the Point class:

public int manhattanDistance(Point other)

Returns the "Manhattan distance" between the current Point object and the given other Point object. The Manhattan distance refers to how far apart two places are if the person can only travel straight horizontally or vertically, as though driving on the streets of Manhattan. In our case, the Manhattan distance is the sum of the absolute values of the differences in their coordinates; in other words, the difference in x plus the difference in y between the points.

Explanation / Answer

public class Point { private int x; private int y; // constructs a new point at the origin, (0, 0) public Point() { this(0, 0); // calls Point(int, int) constructor } // constructs a new point with the given (x, y) location public Point(int x, int y) { setLocation(x, y); } // returns the distance between this Point and (0, 0) public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // returns the x-coordinate of this point public int getX() { return x; } // returns the y-coordinate of this point public int getY() { return y; } // sets this point's (x, y) location to the given values public void setLocation(int x, int y) { this.x = x; this.y = y; } // returns a String representation of this point public String toString() { return "(" + x + ", " + y + ")"; } // shifts this point's location by the given amount public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } public int manhattanDistance(Point other){ /// int distance = Math.abs(x-other) + Math.abs(y-other); return Math.abs(x - other.x)+ Math.abs(y - other.y) ; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote