public int manhattanDistance(Point other) Returns the \"Manhattan distance\" bet
ID: 653711 • Letter: P
Question
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; public int manhattanDistance(Point other) { return (this.x - other.x) + (this.y - other.y); } public static void main(String[] args) { Point p1 = new Point(); Point p2 = new Point(); int distance = p1.manhattanDistance(p2); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.