For this assignment, you will make modifications/additions to your CityStats
and CityLookup
classes from HW4. In particular, you will make the CityStats
class Comparable so that the ArrayList of CityStats
objects in CityLookup
can be sorted. Once they are sorted by cost of living index (with cheapest city at the front and most expensive city at the end), it becomes possible to identify cities by rank and display lists of the least and most expensive cities.
Modify your CityStats
class so that objects of that class are Comparable with each other based on their cost of living index. That is, a city with a lower cost of living should be considered as coming before a city with a higher cost of living. In order to accomplish this, you must:
CityStats
objects. That is:
public class CityStats implements Comparable<CityStats> { . . . }
compareTo
method to your class, which takes another CityStats
object as parameter and returns an integer value based on comparison with that object:
/** * Compares this CityStats object with other using the cost of living index. * @param other the CityStats object being compared with * @return a negative int if the COLI for this city is < the COLI of other, * 0 if the COLI of this city == the COLI of other * a positive int if the COLI for this city is > the COLI of other, */ public int compareTo(CityStats other) { . . . }
CityLookup
class to take advantage of the fact that CityStats
objects can now be ordered. In particular:
CityLookup
so that after reading in and storing all of the values from the file, the ArrayList of CityStats
objects is sorted using Collections.sort
. lookupByRank
that takes one parameter identifying a ranking. If that number is a positive integer, the method should return information on the city with that ranking, starting with the lowest cost of living. For example, if cities
were initialized with the data from COLI2010.txt"
, then the call cities.lookupByRank(2)
would return a String with info on the city with second lowest cost of living index: "2) Pryor Creek, OK: 84.5". If the number is negative, however, the method should return a String with information on the city with that ranking, starting with the highest cost of living. For example, cities.lookupByRank(-4)
would return a String with info on the city with fourth highest cost of living index: "331) San Francisco, CA: 163.5".
showLowest
that takes one parameter, an integer, and displays that many cities, starting with the one with lowest cost of living. For example, if cities
were initialized with the data from COLI2010.txt"
, then the call cities.showLowest(5)
would display the five cities with lowest cost of living indices:
1) Harlingen, TX: 82.9 2) Pryor Creek, OK: 84.5 3) McAllen, TX: 85.2 4) Cookeville, TN: 85.7 5) Pueblo, CO: 85.7
showHighest
that takes one parameter, an integer, and displays that many cities, starting with the one with highest cost of living. For example, if cities
were initialized with the data from COLI2010.txt"
, then the call cities.showHighest(8)
would display the eight cities with highest cost of living indices:
334) New York (Manhattan), NY: 215.4 333) New York (Brooklyn), NY: 180.7 332) Honolulu, HI: 165.4 331) San Francisco, CA: 163.5 330) New York (Queens), NY: 158.2 329) San Jose, CA: 155.8 328) Truckee, CA: 146.6 327) Stamford, CT: 146.4