-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab4.java
More file actions
87 lines (75 loc) · 2.69 KB
/
Lab4.java
File metadata and controls
87 lines (75 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*4. A class called MyPoint, which models a 2D point with x and y coordinates, is designed as follows:
● Two instance variables x (int) and y (int).
● A default (or "no-arg") constructor that construct a point at the default location of (0, 0).
● A overloaded constructor that constructs a point with the given x and y coordinates.
● A method setXY() to set both x and y.
● A method getXY() which returns the x and y in a 2-element int array.
● A toString() method that returns a string description of the instance in the format "(x, y)".
● A method called distance(int x, int y) that returns the distance from this point to another point at the
given (x, y) coordinates
● An overloaded distance(MyPoint another) that returns the distance from this point to the given
MyPoint instance (called another)
● Another overloaded distance() method that returns the distance from this point to the origin (0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint) to test all the
methods defined in the class. */
class Mypoint {
int x, y;
Mypoint() {
x = 0;
y = 0;
}
Mypoint(int x, int y) {
this.x = x;
this.y = y;
}
void setXY(int x, int y) {
this.x = x;
this.y = y;
}
int[] getXY() {
int a[] = new int[2];
a[0] = x;
a[1] = y;
return a;
}
public String toString() {
return "(" + x + "," + y + ")";
}
double Distance(int x, int y) {
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
}
double Distance() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
double Distance(Mypoint P2) {
return Math.sqrt(Math.pow(this.x - P2.x, 2) + Math.pow(this.y - P2.y, 2));
}
}
public class Lab4 {
public static void main(String[] args) {
Mypoint P1 = new Mypoint();
Mypoint P2 = new Mypoint(10, 20);
int a[] = P1.getXY();
System.out.println("P1 X =" + a[0] + "\nP1 Y =" + a[1]);
P1.setXY(5, 6);
System.out.println("Point p1 =");
System.out.println(P1);
System.out.println("point p2=");
System.out.println(P2);
System.out.println("Distance between the point P1 & (2,3) is=" + P1.Distance(2, 3));
System.out.println("Distance from P1 to origin =" + P1.Distance());
System.out.println("Distance between P1 & P2 =" + P1.Distance(P2));
}
}
/*
* Output:
* P1 X =0
* P1 Y =0
* Point p1 =
* (5,6)
* point p2=
* (10,20)
* Distance between the point P1 & (2,3) is=4.242640687119285
* Distance from P1 to origin =7.810249675906654
* Distance between P1 & P2 =14.866068747318506
*/