-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointclient.java
79 lines (72 loc) · 1.88 KB
/
pointclient.java
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
publicÊclassÊpointclientÊ{
ÊÊÊÊpublicÊstaticÊvoidÊmain(String[]Êargs)Ê{
ÊÊÊÊÊÊÊÊ//ÊconstructÊtwoÊPointÊobjects,ÊoneÊatÊ(8,Ê2)ÊandÊoneÊatÊ(4,Ê3)
Point p1 = new Point(8,2);
Point p2 = new Point(4,3);
ÊÊÊÊÊÊÊÊ//ÊdisplayÊtheÊtwoÊPointÊobjects'Êstate
ÊÊÊÊÊÊÊÊSystem.out.println("p1ÊisÊ("+p1.getX()+", "+p1.getY()+")");
ÊÊÊÊÊÊÊÊSystem.out.println("p2ÊisÊ("+p2.getX()+", "+p2.getY()+")");
ÊÊÊÊÊÊÊÊ//ÊdisplayÊp1ÊdistanceÊfromÊorigin
ÊÊÊÊÊÊÊÊSystem.out.println("p1'sÊdistanceÊfromÊoriginÊisÊ"+p1.distanceFromOrigin());
ÊÊÊÊÊÊÊÊ//ÊtranslateÊp1ÊtoÊ(9,Ê4)
p1.translate(1,Ê2);
ÊÊÊÊÊÊÊÊ//ÊtranslateÊp2ÊtoÊ(3,Ê13)
p2.setLocation(3,Ê13);
ÊÊÊÊÊÊÊÊ//ÊdisplayÊtheÊtwoÊPointÊobjects'ÊstateÊagain
ÊÊÊÊÊÊÊÊSystem.out.println("p1ÊisÊnowÊ("+p1.getX()+", "+p1.getY()+")");
ÊÊÊÊÊÊÊÊSystem.out.println("p2ÊisÊnowÊ("+p2.getX()+", "+p2.getY()+")");
ÊÊÊÊ}
}
//Ê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);
ÊÊÊÊ}
}