Given the following code fragment public class Point { public int x; // Because
ID: 651207 • Letter: G
Question
Given the following code fragment
public class Point {
public int x; // Because these are public, you can access them public int y; // directly without getters and setters
}
(a) Write a method equals for the Rectangle class that takes a Rectangle as a parameter and returns true if two rectangles have the same area, and false otherwise
(b) Write a method manhattanDistance for the Point class that takes a Point object and calculates the distance between the current Point and the the given other Point assuming travel only along horizontal and vertical lines (e.g. between the manhattan distance between (0,0) and (1,1) would be 2)
Explanation / Answer
public boolean equal_area(Rectangle r1,Rectangle r2){
int r1_length = r1.getURPoint().x - r1.getLLPoint().x;
int r1_breadth = r1.getLLPoint().y - r1.getURPoint().y;
int r1_area = r1_length*r1_breadth;
int r2_length = r2.getURPoint().x - r2.getLLPoint().x;
int r2_breadth = r2.getLLPoint().y - r2.getURPoint().y;
int r2_area = r2_length*r2_breadth;
if (r1_area == r2_area)
return true;
return false;
}
public int manhattanDistance(Point p){
return abs(p.x - x) + abs(p.y - y);
// abs is absolute value
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.