Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions