Add the following method to the Point class: public int manhattanDistance(Point
ID: 3765589 • 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. public class Point { private int x; private int y; // your code goes here }
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.