-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
91 lines (81 loc) · 2.26 KB
/
Link.java
File metadata and controls
91 lines (81 loc) · 2.26 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
/* Class representing a link between cities
*/
public class Link implements Comparable<Link> {
public City city1;
public City city2;
public int length;
/* true if and only if this link is part of the set of shortest paths */
public boolean used;
public String color = "";
/* Construct a Link between c1 and c2 with length len
* The City alphanumerically smaller is stored as city1 and the other will be city2
* add the link to both cities
*/
public Link(City c1, City c2, int len) {
if (c1.compareTo(c2) < 0) {
city1 = c1;
city2 = c2;
} else {
city1 = c2;
city2 = c1;
}
length = len;
c1.addLink(this);
c2.addLink(this);
used = true;
}
public Link(City c1, City c2, int len, String color){
if (c1.compareTo(c2) < 0) {
city1 = c1;
city2 = c2;
} else {
city1 = c2;
city2 = c1;
}
length = len;
c1.addLink(this);
c2.addLink(this);
//c1.addColorLink(this.color);
this.color = color;
used = true;
}
/* return the length of this link */
public int getLength() {
return length;
}
/* get the opposite city from c
* return city1 if c is city2
* return city2 if c is city1
* behaviour is unspecified if c is not city1 or city2
*/
public City getAdj(City c) {
return c == city1 ? city2 : city1;
}
/* return true if this link is on a shortest path and false otherwise */
public boolean isUsed() {
return used;
}
/* set used to u */
public void setUsed(boolean u) {
used = u;
}
/* return a string representation of the Link
* e.g. "City1 3 City2"
* The city names should be in sorted order, e.g. Halifax comes before Toronto
*/
public String toString() {
return city1.toString() + " " + length + " " + city2.toString();
}
/* compare this Link to Link l
* returns 0 if both links have the same city1 and city2
* return negative int if this.city1 < l.city1 or the city1 are equal and this.city2 < l.city2
* else return a positive int
*/
public int compareTo(Link l) {
int diff = city1.compareTo(l.city1);
if (diff == 0) {
diff = city2.compareTo(l.city2);
}
return diff;
}
}