blob: d5273ba13308bed8c95af86e641aca53fc59cfc4 (
plain)
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
|
/**
* The Region class represents a geographical location with a name, population, and cost.
* It provides methods to retrieve the population and cost of the location,
* as well as a method to generate a string representation of the object.
*/
public class Region {
private String name;
private int population;
private double baseCost;
/**
* Creates a new Region object with the given name, population, and cost.
* @param name the name of the location
* @param pop the population of the location
* @param baseCost the base cost of the location
*/
public Region(String name, int pop, double baseCost) {
this.name = name;
this.population = pop;
this.baseCost = baseCost;
}
/**
* Returns the population of the location
* @return the population of the location
*/
public int getPopulation() { return this.population; }
/**
* Returns the cost of the location
* @param index a number indicating when this region is provided relief. A larger value for
* index indicates that this region is helped later.
* @return the cost of providing relief to this region. Regions that are
* helped later (i.e. with a larger index value) have a higher cost.
*/
public double getCost(int index) {
return (1 + 0.1 * index) * this.baseCost;
}
/**
* Returns a String representation of a Region object in the format:
* "<name>: pop. <population>, cost: $<cost>"
* @return the String representation of a Region object
*/
public String toString() {
return name + ": pop. " + population + ", base cost: $" + baseCost;
}
/**
* Compares the specified object with this location for equality. Returns true if the
* specified object is also a location and the two locations have the
* same name, population, and cost.
* @param other object to be compared for equality with this location
* @return true if the specified object is equal to this location
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Region)) {
return false;
}
Region otherLoc = (Region)other;
return this.name.equals(otherLoc.name) &&
this.population == otherLoc.population &&
this.baseCost == otherLoc.baseCost;
}
/**
* Returns the hash code value for this location
* @return the hash code value for this location
*/
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + Integer.hashCode(population);
result = 31 * result + Double.hashCode(baseCost);
return result;
}
}
|