Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 2(50 points): Following the instructions in the problem statement, desig

ID: 3713012 • Letter: P

Question

Program 2(50 points): Following the instructions in the problem statement, design and implement a Java program for programming exercise 10.4, page 400 (name it MyPoint.java). In addition to the methods required by the textbook instructions, include getter methods for the x- and y-coordinates and a tostring ) method which returns the coordinates of the MyPoint object following the model (0.0, 0.0) Also, the coordinates of a MyPoint object must not be able to be modified after the object is created. Next, develop a test program in a separate file (call it TestMyPoint.java) to test all methods of the class Document your code and organize the output using appropriate formatting techniques.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


MyPoint.java
--------

public class MyPoint {
private double x;
private double y;

public MyPoint()
{
x = 0;
y = 0;
}

public MyPoint(double x1, double y1)
{
x = x1;
y = y1;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double distance(MyPoint p)
{
double dx = x - p.x;
double dy = y - p.y;

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

public double distance(double x2, double y2)
{
double dx = x - x2;
double dy = y - y2;

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

public String toString()
{
return "(" + x +", " + y + ")";
}
}


TestMyPoint.java
--------

public class TestMyPoint {
public static void main(String[] args) {
MyPoint p1 = new MyPoint(0, 0);
MyPoint p2 = new MyPoint(10, 30.5);

System.out.println("Point p1: " + p1);
System.out.println("Point p2: " + p2);

System.out.println("Testing getter methods");
System.out.println("p2.getX() = " + p2.getX());
System.out.println("p2.getY() = " + p2.getY());


System.out.println(" Testing distance methods");
System.out.printf("Distance between p1 and p2 is %.2f " , p1.distance(p2));
System.out.printf("Distance between p2 and (5, 8) is %.2f " , p2.distance(5, 8));

}
}


output
-----
Point p1: (0.0, 0.0)
Point p2: (10.0, 30.5)
Testing getter methods
p2.getX() = 10.0
p2.getY() = 30.5

Testing distance methods
Distance between p1 and p2 is 32.10
Distance between p2 and (5, 8) is 23.05

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote