-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathPoint.java
More file actions
69 lines (56 loc) · 1.56 KB
/
Point.java
File metadata and controls
69 lines (56 loc) · 1.56 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
package coordinate;
import java.util.Objects;
public class Point {
private final int x;
private final int y;
private Point(int x, int y) {
this.x = x;
if (x < 0 || x > 24) {
throw new IllegalArgumentException();
}
this.y = y;
if (y < 0 || y > 24) {
throw new IllegalArgumentException();
}
}
public double getDistance(Point other) {
int xDifference = other.minusX(x);
int yDifference = other.minusY(y);
return Math.sqrt(square(xDifference) + square(yDifference));
}
private int minusX(int number) {
return this.x - number;
}
private int minusY(int number) {
return this.y - number;
}
private static int square(int number) {
return number * number;
}
public static Point of(int x, int y) {
return new Point(x, y);
}
public static Point ofCommaSeparator(String text) {
String[] values = text.split(",");
return new Point(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x &&
y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}