Question: Given Point.java and PointMain.java from the book, begin by properly documenting the code. Addto the Point class: public double distance(Point other) Returns the distance between

Given Point.java and PointMain.java from the book, begin by properly documenting the code.

Addto the Point class:

public double distance(Point other)

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

(x2  x1)^2 + (y2  y1)^2

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:

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 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:

public int quadrant()

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:

// 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; } }

PointMain.java:

// A program that deals with 2D points. // Second version, to accompany Point class with behavior. public class PointMain { public static void main(String[] args) { // create two Point objects Point p1 = new Point(); p1.x = 7; p1.y = 2; Point p2 = new Point(); p2.x = 4; p2.y = 3; // 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 + ")"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!