Given Point.java and PointMain.java from the book, begin by properly documenting
ID: 3707767 • Letter: G
Question
Given Point.java and PointMain.java from the book, begin by properly documenting the code.
Addto the Point class:
Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y-coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of
Two points with the same (x, y) coordinates should return a distance of 0.0. Add code to PointMain to test the method (call method on points).Add the following accessor method to the Point class:
Returns the “Manhattan distance” between the current Point object and the given other Point object. The Manhattan distance refers to the distance between two places if one can travel between them only by moving 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. Add code to PointMain to test the method (call method on points).Add the following accessor method to the Point class:
Returns which quadrant of the x/y plane the current Point object falls in. Quadrant 1 contains all points whose x and y values are both positive. Quadrant 2 contains all points with negative x but positive y. Quadrant 3 contains all points with negative x and y values. Quadrant 4 contains all points with positive x but negative y. If the point lies directly on the x and/or y axis, return 0. Add code to PointMain to test the method (call method on points).
Point.java:
PointMain.java:
Explanation / Answer
Point.java
// A Point object represents a pair of (x, y) coordinates.
// Second version: state and behavior.
public class Point {
int x;
int y;
// Returns the distance between this point and (0, 0).
public double distanceFromOrigin() {
return Math.sqrt(x * x + y * y);
}
// Shifts this point's location by the given amount.
public void translate(int dx, int dy) {
x += dx;
y += dy;
}
//Returns the distance between the current Point object and the given other Point object.
public double distance(Point other) {
return Math.sqrt(((x - other.x) * (x - other.x)) + ((y - other.y) * (y - other.y)));
}
//Returns the “Manhattan distance” between the current Point object and the given other Point object.
public int manhattanDistance(Point other) {
return Math.abs(x - other.x) + Math.abs(y - other.y);
}
// Returns which quadrant of the x/y plane the current Point object falls in.
public int quadrant() {
if ((x == 0) || (y == 0)) { //point on axes
return 0;
}
else if ((x > 0) && (y > 0)) { //1st quadrant
return 1;
}
else if ((x < 0) && (y > 0)) { //2nd quadrant
return 2;
}
else if ((x < 0) && (y < 0)) { //3rd quadrant
return 3;
}
else { //4th quadrant
return 4;
}
}
}
////////////////////////// END OF Point.java ////////////////////
PointMain.java:
// create two Point objects
Point p1 = new Point();
p1.x = 7;
p1.y = 2;
Point p2 = new Point();
p2.x = 4;
p2.y = 3;
Point p3 = new Point();
p3.x = 0;
p3.y = 0;
// print each point and its distance from origin
System.out.println("p1 is (" + p1.x + ", " + p1.y + ")");
System.out.println("distance from origin = " +
p1.distanceFromOrigin());
System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");
System.out.println("distance from origin = " +
p2.distanceFromOrigin());
// translate each point to a new location
p1.translate(11, 6);
p2.translate(1, 7);
// print the points again
System.out.println("p1 is (" + p1.x + ", " + p1.y + ")");
System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");
// print the distance between p1 and p2
System.out.println("Distance between p1 and p2 is: " + p1.distance(p2));
System.out.println("Distance between p1 and p1 is: " + p1.distance(p1));
// print the manhattan distance between p1 and p2
System.out.println("Manhattan Distance between p1 and p2 is: " + p1.manhattanDistance(p2));
System.out.println("Manhattan Distance between p1 and p1 is: " + p1.manhattanDistance(p1));
// print the quadrants
System.out.println("Quadrant of p1 is " + p1.quadrant());
System.out.println("Quadrant of p2 is " + p2.quadrant());
System.out.println("Quadrant of p3 is " + p3.quadrant());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.