Home Java Java-tips Data Collections Exercises Collections Exercise 2 - State Capitals

Ask Questions?

View Latest Questions

Advertisement


 
 

Collections Exercise 2 - State Capitals
Posted on: July 26, 2006 at 12:00 AM
For the purposes of this exercise, the only data structures that need to be considered are: array, ArrayList, LinkedList, HashSet, TreeSet, HashMap, and TreeMap.

Java: Collections Exercise 2 - State Capitals

Name ____________________________________

For the purposes of this exercise, the only data structures that need to be considered are: array, ArrayList, LinkedList, HashSet, TreeSet, HashMap, and TreeMap. For passing over the elements of a data structure, consider only Iterator and for loop.

Situation: You need to write a function that, given a state abbreviation code (a String), returns the capital city of that state (a String). You may assume there are exactly 50 state codes.

Question: What data structure would be appropriate, and how could this data structure be used? Show the following:

  1. The data structure instance variable, m_stateCapitals, declaration and constructor call.
  2. Data initialization: Show how to add one state (eg, "MI" with capital "Lansing").
  3. A method, private String capital(String stateCode), that returns the capital city associated with the corresponding state code..
Copyleft 2004 Fred Swartz MIT License
Advertisement


DMCA.com