forked from ChrisMayfield/ThinkJava2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularPolygon.java
More file actions
30 lines (28 loc) · 885 Bytes
/
Copy pathRegularPolygon.java
File metadata and controls
30 lines (28 loc) · 885 Bytes
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
// Exercise 17.1
public static final String[] NAMES = {null, null, null,
"Triangle", "Square", "Pentagon", "Hexagon",
"Heptagon", "Octagon", "Nonagon", "Decagon"};
@Override
public String toString() {
StringBuilder str = new StringBuilder();
if (npoints < NAMES.length) {
str.append(NAMES[npoints]);
} else {
str.append("Polygon");
}
// append the list of vertexes
str.append('[');
for (int i = 0; i < npoints; i++) {
if (i > 0) {
str.append(", ");
}
// append the next (x, y) point
str.append('(');
str.append(xpoints[i]);
str.append(", ");
str.append(ypoints[i]);
str.append(')');
}
str.append(']');
return str.toString();
}