-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointFactory.java
59 lines (53 loc) · 1.5 KB
/
PointFactory.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
enum Quadrant {
Q1, // x +ve, y +ve
Q2, // x -ve, y +ve
Q3, // x -ve, y -ve
Q4; // x +ve, y -ve
}
interface GridQuadrant {
Quadrant getQuadrant(); // return which quadrant the point is in
// if point is at (0,0) return null
}
// TODO: modify Point to implement GridQuadrant
interface CompareQuadrant {
boolean isInSameQuadrant(Point p); // true if given point is
// in the same quadrant as this point
}
// TODO: modify Point to implement CompareQuadrant
class Point implements GridQuadrant, CompareQuadrant {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public Quadrant getQuadrant() {
if (x > 0 && y > 0) return Quadrant.Q1;
if (x < 0 && y > 0) return Quadrant.Q2;
if (x < 0 && y < 0) return Quadrant.Q3;
if (x > 0 && y < 0) return Quadrant.Q4;
return null;
}
public boolean isInSameQuadrant(Point p) {
return getQuadrant() == p.getQuadrant();
}
}
interface PointMaker {
Point makePoint(double x, double y); // returns point with given x,y
int countPointsCreated(); // returns count of points created via factory
}
class PointFactory implements PointMaker {
private int count = 0;
public Point makePoint(double x, double y) {
count++;
return new Point(x, y);
}
public int countPointsCreated() {
return count;
}
}