-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
168 lines (153 loc) · 4.8 KB
/
Point.java
File metadata and controls
168 lines (153 loc) · 4.8 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Represents a point with a x and y component.
*/
class Point {
private int x;
private int y;
/**
* Initializes a Point object with the given x and y coordinates.
*
* @param X The x-coordinate of the point.
* @param Y The y-coordinate of the point.
*/
public Point(int X, int Y) {
this.x = X;
this.y = Y;
}
/**
* Initializes a Point object with the given character-based x and integer-based y coordinates.
*
* @param X The character-based x-coordinate of the point.
* @param Y The integer-based y-coordinate of the point.
*/
public Point(char X, int Y) {
this.x = Character.toUpperCase(X) - 'A';
this.y = Y;
}
/**
* Returns a new Point object that is the sum of this point and another given point.
*
* @param other The Point object to add to this point.
* @return The resulting Point object after addition.
*/
public Point add(Point other) {
return new Point(x + other.x, y + other.y);
}
/**
* Returns a new Point object that is the difference between this point and another given point.
*
* @param other The Point object to subtract from this point.
* @return The resulting Point object after subtraction.
*/
public Point subtract(Point other) {
return new Point(x - other.x, y - other.y);
}
/**
* Returns a reference to this Point object.
*
* @return This Point object.
*/
public Point clone() {
return this;
}
/**
* Returns the x-coordinate of this Point object.
*
* @return The x-coordinate of this Point object.
*/
public int getX() {
return x;
}
/**
* Returns the y-coordinate of this Point object.
*
* @return The y-coordinate of this Point object.
*/
public int getY() {
return y;
}
/**
* Sets the x-coordinate of this Point object to a new value.
*
* @param newX The new value for the x-coordinate of this Point object.
*/
public void setX(int newX) {
x = newX;
}
/**
* Sets the y-coordinate of this Point object to a new value.
*
* @param newY The new value for the y-coordinate of this Point object.
*/
public void setY(int newY) {
x = newY;
}
/**
* Returns a string representation of this Point object in the form "(x, y)".
*
* @return A string representation of this Point object.
*/
public String toString() {
return String.format("(%d, %d)", x, y);
}
/**
* Returns true if the given string is in the format of a Point object.
*
* @param point The string to check for Point format.
* @return True if the string is in Point format, false otherwise.
*/
public static boolean isPointFormat(String point) {
return point.strip().matches("[A-Za-z](\\s+)?[0-9]");
}
/**
*
*@param point A string representation of a point in format "A 1"
*@return A new instance of class Point with coordinates from input string
*@throws IllegalArgumentException if input string has invalid format
*/
public static Point valueOf(String point) throws IllegalArgumentException {
String pointString = point.strip();
if (isPointFormat(point)) {
return new Point(pointString.charAt(0), Integer.valueOf(pointString.substring(pointString.length() - 1, pointString.length())));
}
throw new IllegalArgumentException("Invalid Point Format");
}
/**
* Returns a hash code value for this Point object.
*
* @return A hash code value for this Point object.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
/**
* Returns true if this Point object is equal to another given object.
*
* @param obj The object to compare with this Point object.
* @return True if the two objects are equal, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public static void main(String[] args) {
Point boby = Point.valueOf(" E 5 ");
System.out.println(boby);
}
}