Question: The PointClient program is supposed to construct two Point objects, translate each, and then print their coordinates. The expected output is shown below. Finish the

The PointClient program is supposed to construct two Point objects, translate each, and then print their coordinates. The expected output is shown below. Finish the program so that it runs properly.

p1 is (8, 2) p2 is (4, 3) p1's distance from origin is 8.246211251235321 p1 is now (9, 4) p2 is now (3, 13) 

public class PointClient {

public static void main(String[] args) {

// construct two Point objects, one at (8, 2) and one at (4, 3)

// display the two Point objects' state

System.out.println("p1 is " ...);

System.out.println("p2 is " ...);

// display p1 distance from origin

System.out.println("p1's distance from origin is " ...);

// translate p1 to (9, 4)

// translate p2 to (3, 13)

// display the two Point objects' state again

System.out.println("p1 is now " ...);

System.out.println("p2 is now " ...);

}

}

// A Point object represents a pair of (x, y) coordinates.

// (Point class must be submitted with your solution; do not modify!)

public class Point {

public int x;

public int y;

public Point() {

setLocation(0, 0);

}

public Point(int x, int y) {

setLocation(x, y);

}

public double distanceFromOrigin() {

return distance(new Point());

}

public double distance(Point p) {

int dx = x - p.x;

int dy = y - p.y;

return Math.sqrt(dx * dx + dy * dy);

}

public final int getX() {

return x;

}

public final int getY() {

return y;

}

public void setLocation(int x, int y) {

this.x = x;

this.y = y;

}

public void setX(int x) {

this.x = x;

}

public void setY(int y) {

this.y = y;

}

public String toString() {

return "(" + x + ", " + y + ")";

}

public void translate(int dx, int dy) {

setLocation(x + dx, y + dy);

}

}

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!