-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBar.java
More file actions
58 lines (50 loc) · 1.79 KB
/
Copy pathBar.java
File metadata and controls
58 lines (50 loc) · 1.79 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
import java.util.Arrays;
public class Bar implements Comparable<Bar> {
private String cityName, cat;
private int val;
// Creates a new bar.
public Bar(String name, int value, String category) {
cityName = name;
val = value;
cat = category;
if (name == null || category == null || value < 0)
throw new IllegalArgumentException(
"value is negative || name or(and) category is null");
}
// Returns the name of this bar.
public String getName() {
return cityName;
}
// Returns the value of this bar.
public int getValue() {
return val;
}
// Returns the category of this bar.
public String getCategory() {
return cat;
}
// Compare two bars by value.
public int compareTo(Bar that) {
if (that == null)
throw new NullPointerException("arguement is null!");
return (val - that.val);
}
// Sample client (see below).
public static void main(String[] args) {
// create an array of 10 bars
Bar[] bars = new Bar[10];
bars[0] = new Bar("Beijing", 22674, "East Asia");
bars[1] = new Bar("Cairo", 19850, "Middle East");
bars[2] = new Bar("Delhi", 27890, "South Asia");
bars[3] = new Bar("Dhaka", 19633, "South Asia");
bars[4] = new Bar("Mexico City", 21520, "Latin America");
bars[5] = new Bar("Mumbai", 22120, "South Asia");
bars[6] = new Bar("Osaka", 20409, "East Asia");
bars[7] = new Bar("São Paulo", 21698, "Latin America");
bars[8] = new Bar("Shanghai", 25779, "East Asia");
bars[9] = new Bar("Tokyo", 38194, "East Asia");
// sort in ascending order by weight
Arrays.sort(bars);
System.out.println(bars[0].getName());
}
}