SA-9, due Oct 25
Assignment
This assignment will give you practice with sets and maps. The program
StatesAndCities.java is the skeleton of
a program that allows you to add
(state, city) pairs to a database of such pairs. They are stored in a Map
keyed on state names. The corresponding value is a Set of city names
that are within the state. I have replaced method bodies by the comment:
// *** Your Code Here ***.
Your job is to complete the method bodies to make this program work correctly.
Helpful notes
-
HashMapis a class injava.utilthat implements theMapinterface. Thus, to create an empty map where each key is a string (for the state name) and each value is a set of strings (for the names of cities in the state), you write:Map > x = new HashMap >(); -
HashSetis a class injava.utilthat implements theSetinterface. -
If
xis your map (that maps a state to a set of cities),m.keySet() returns the set of keys inx. You can obtain an iterator on this set (using theiterator()method ofSet), and then use the iterator'shasNext()andnext()methods to obtain each key (i.e., each state) in the mapx. -
You can consult the online Oracle's documentation to see
all the methods supported by the
MapandSetinterfaces.
Sample run
A sample run of the finished program is:
Extra Credit
There are many types of elections. In class we looked at instant runoff voting, but there are also regular voting (only first vote on ballot counts; winner is the candidate with the most votes) and approval voting (all candidates on the ballot get a vote; winner is the candidate with the most votes). Modify InstantRunoffOO.java, Ballot.java, Election.java, and VoteTally.java to handle these types of elections as well. After a set of ballots has been read report the winner under each of the three systems.
Blackboard submission
Submit via Blackboard the zip file of a folder containing (1) your StatesAndCities.java, and
(2) a screenshot of your test run with convincing test cases
(test your code by running the main method of StatesAndCities.java.