-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildings.java
More file actions
144 lines (129 loc) · 3.41 KB
/
Copy pathBuildings.java
File metadata and controls
144 lines (129 loc) · 3.41 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
package hw8;
/**
* Buildings is an immutable class which contains the location and name of
* buildings and end points of path segments.
*
*
* @author RibhavHora
*/
public class Buildings {
// x is the x coordinate of the location in pixels
private double x;
// y is the y coordinate of the location in pixels
private double y;
// longName stores the name of the building
private String longName;
// shortName stores the abbreviated name of the building
private String shortName;
// Abstraction Function :
// x and y represent the location of an end point
// longName and shortName represent the name of the building
// and are empty strings if it is an end point which is not a building
//
// Representation function:
// longName, shortName cannot be null
/**
* Constructs a new Buildings with values
*
* @param x
* the x coordinate of the pixels
* @param y
* the y coordinate of the pixels
* @param longName
* name of the building or empty if not a building
* @param shortName
* abbreviated name of the building or empty if not a building
* @effects Constructs a new Buildings with coordinates and name if any
*/
public Buildings(double x, double y, String longName, String shortName) {
this.x = x;
this.y = y;
this.longName = longName;
this.shortName = shortName;
checkRep();
}
/**
* Constructs an empty Buildings
*
* @effects Constructs a new empty Buildings
*/
public Buildings() {
this.x = 0.0;
this.y = 0.0;
this.longName = "";
this.shortName = "";
}
/**
* Gives the x coordinate of the buildings
*
* @returns the x coordinate of the buildings
*/
public double getX() {
return x;
}
/**
* Gives the y coordinate of the buildings
*
* @returns the y coordinate of the buildings
*/
public double getY() {
return y;
}
/**
* Gives the full name of the end point if it is a building
*
* @returns the full name of the end point if it is a building or empty
* string
*/
public String getLong() {
return longName;
}
/**
* Gives the abbreviated name of the end point if it is a building
*
* @returns the abbreviated name of the end point if it is a building or
* empty string
*/
public String getShort() {
return shortName;
}
/**
* toString in the format (x, y) where x and y are rounded to the nearest
* integer
*
* @returns string of a buildings in the format (x, y) where x and y are
* rounded to the nearest integer
*/
public String toString() {
return "(" + Math.round(x) + ", " + Math.round(y) + ")";
}
/**
* Compares two buildings
*
* @returns true if other Buildings object has the same position
*/
@Override
public boolean equals(Object e2) {
if (!(e2 instanceof Buildings))
return false;
Buildings b = (Buildings) e2;
if (this.getLong().equals("") || b.getLong().equals("")) // comapring two non buildings
return (this.getX() == b.getX() && this.getY() == b.getY());
else
return (this.getX() == b.getX() && this.getY() == b.getY()) || (this.getShort().equals(b.getShort()))
|| (this.getLong().equals(b.getLong()));
}
/**
* Gives a hashCode for Buildings
*
* @returns a hashCode for a Buildings
*/
public int hashCode() {
return (int) x + (int) y;
}
// makes sure the rep invariant does not break
private void checkRep() {
assert !longName.equals(null);
assert !shortName.equals(null);
}
}