Pages

Saturday, 11 June 2011

Mehran Sahami Handout #41 CS 106A November 26, 2007


Mehran Sahami Handout #41
CS 106A November 26, 2007
Section Handout #8: Data Structures
Parts of this handout by Brandon Burr and Patrick Young
Your task for this section is to write a program that reads in a file containing flight destinations from various cities, and then allow the user to plan a round-trip flight route.
Here’s what a sample run of the program might look like:
– 2 –
The flight data come from a file named flights.txt, which has the following format:
• Each line consists of a pair of cities separated by an arrow indicated by the two character combination ->, as in
New York -> Anchorage
• The file may contain blank lines for readability (you should just ignore these).
The entire data file used to produce this sample run appears below.
San Jose -> San Francisco
San Jose -> Anchorage
New York -> Anchorage
New York -> San Jose
New York -> San Francisco
New York -> Honolulu
Anchorage -> New York
Anchorage -> San Jose
Honolulu -> New York
Honolulu -> San Francisco
Denver -> San Jose
San Francisco -> New York
San Francisco -> Honolulu
San Francisco -> Denver
Your program should:
• Read in the flight information from the file flights.txt and store it in an appropriate data structure.
• Display the complete list of cities.
• Allow the user to select a city from which to start.
• In a loop, print out all the destinations that the user may reach directly from the current city, and prompt the user to select the next city.
• Once the user has selected a round-trip route (i.e., once the user has selected a flight that returns them to the starting city), exit from the loop and print out the route that was chosen.
A critical issue in building this program is designing appropriate data structures to keep track of the information you'll need in order to produce flight plans. You'll need to both have a way of keeping track of information on available flights that you read in from the flights.txt file, as well as a means for keeping track of the flight routes that the user is choosing in constructing their flight plan. Consider how both ArrayLists and HashMaps might be useful to keep track of the information you care about.

0 comments:

Post a Comment

Search This Blog