-
Notifications
You must be signed in to change notification settings - Fork 13
/
Point.java
70 lines (53 loc) · 1.65 KB
/
Point.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
package lessons.oop.basics.examples.point;
public class Point {
public int x;
public int y;
public double distance(Point point)
{
return Math.sqrt((Math.pow(x - point.x, 2) + Math.pow(y - point.y, 2)));
}
public static double distanceWithTwoPoint(Point p1, Point p2)
{
return p1.distance(p2);
}
}
class PointFactory {
public static Point createPoint(int x, int y)
{
Point p = new Point();
p.x = x;
p.y = y;
return p;
}
public static Point createPoint(java.util.Scanner kb)
{
System.out.print("x:");
int x = kb.nextInt();
System.out.print("y:");
int y = kb.nextInt();
return createPoint(x, y);
}
public static Point createPoint(java.util.Random random)
{
System.out.print("x:");
int x = random.nextInt(10);
System.out.print("y:");
int y = random.nextInt(10);
return createPoint(x, y);
}
}
class Test {
public static void main(String[] args)
{
Point p1 = PointFactory.createPoint(new java.util.Scanner(System.in));
Point p2 = PointFactory.createPoint(new java.util.Scanner(System.in));
System.out.println(p1.distance(p2));
System.out.println(p1.distance(p1));
System.out.println(Point.distanceWithTwoPoint(p1, p2));
System.out.println(Point.distanceWithTwoPoint(p2, p2));
Point p3 = PointFactory.createPoint(new java.util.Random());
Point p4 = PointFactory.createPoint(new java.util.Random());
System.out.println(p3.distance(p4));
System.out.println(Point.distanceWithTwoPoint(p3, p4));
}
}