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.
HashMap
is a class in java.util
that implements the Map
interface.
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:
HashSet
is a class in java.util
that implements the Set
interface.
x
is your map (that maps a state to a set of cities),
x
.
You can obtain an iterator on this set (using the iterator()
method of Set
),
and then use the iterator's hasNext()
and next()
methods
to obtain each key (i.e., each state) in the map x
.
Map
and Set
interfaces.
A sample run of the finished program is:
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.
Submit via Canvas your StatesAndCities.java
, and
a screenshot or copied file of your test run with convincing test cases.
Test your code by running the main
method of StatesAndCities.java
.