LabProgram4.java (1)
LabProgram4.java (1)
Solution:
package p4;
class MyPoint{
int x, y;
//Parameterized constructor
MyPoint (int x, int y) {
this.x = x;
this.y = y;
}
//Set x and y
void setXY (int x, int y) {
this.x = x;
this.y = y;
}
d = Math.sqrt(Math.pow((x2-x),2) + Math.pow((y2-y),2) );
return d;
}
double distance(MyPoint p) {
double d;
d = Math.sqrt(Math.pow((p.x-x),2) + Math.pow((p.y-y),2) );
return d;
}
double distance() {
double d;
d = Math.sqrt(Math.pow((x-0),2) + Math.pow((y-0),2) );
//d = Math.sqrt(Math.pow((x),2) + Math.pow((y),2) );
return d;
}
//2. Find distance between current point and other point object
dist = p2.distance(p3);
pa2 = p2.getXY();
pa3 = p3.getXY();
System.out.printf("2. 8Distance between (%d, %d) to (%d, %d):
%5.2f \n", pa2[0], pa2[1], pa3[0], pa3[1], dist);
} //end main
} //end class PointDemo
Output:
1. Distance between (2, 4) to (1, 2): 2.24
2. Distance between (2, 4) to (2, 3): 1.00
3. Distance between (2, 4) to (0, 0): 4.47