Program 2 (50 points): Following the instructions in the problem statement, desi
ID: 3708555 • 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
TestMyPoint.java
public class TestMyPoint {
public static void main(String[] args) {
MyPoint m1 = new MyPoint(0.0,0.0);
System.out.println(m1);
MyPoint m2 = new MyPoint(2.0,4.0);
System.out.println(m2);
}
}
MyPoint.java
public class MyPoint {
private double x,y;
public MyPoint(){
x =0;
y=0;
}
public MyPoint(double x, double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public String toString() {
return "("+x+", "+y+")";
}
}
Output:
(0.0, 0.0)
(2.0, 4.0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.