Docsity

Data Structures - Section Handout - Programming Methodology- 41, Exercises of Programming Methodologies

Partial preview of the text

Download Data Structures - Section Handout - Programming Methodology- 41 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

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.

  • 2 – /**
  • Given a starting city, looks up the destinations from that
  • city in the HashMap of flights, and return an array list of
  • the destinations that are available. / private ArrayList getDestinations(String fromCity) { return flights.get(fromCity); } /*
  • Prints a list of cities from the array list. Each city name is
  • indented by a space. / private void printCityList(ArrayList cityList) { for(int i = 0; i < cityList.size(); i++) { String city = cityList.get(i); println(" " + city); } } /*
  • Given a list of city names, prints out the flight
  • route, with a " -> " between each pair of cities / private void printRoute(ArrayList route) { println("The route you've chosen is: "); for (int i = 0; i < route.size(); i++) { if (i > 0) print(" -> "); print(route.get(i)); } println(); } /*
  • Reads in the city information from the given file storing the
  • information in both the ArrayList of cities and the HashMap of
  • flights. */ private void readFlightData(String filename) { flights = new HashMap<String,ArrayList>(); cities = new ArrayList(); try { BufferedReader rd = new BufferedReader(new FileReader(filename)); while (true) { String line = rd.readLine(); if (line == null) break; if (line.length() != 0) { readFlightEntry(line); } } rd.close(); } catch (IOException ex) { throw new ErrorException(ex); } }
  • 3 – /**
  • Reads a single flight entry from the line passed as an argument,
  • which should be in the form
  • fromCity -> toCity
  • Each new city is added to the ArrayList cities, and each new
  • flight is recorded by adding a new destination city to the
  • ArrayList stored in the HashMap flights under the key for the
  • starting city. / private void readFlightEntry(String line) { int arrow = line.indexOf("->"); if (arrow == -1) { throw new ErrorException("Illegal flight entry " + line); } String fromCity = line.substring(0, arrow).trim(); String toCity = line.substring(arrow + 2).trim(); defineCity(fromCity); defineCity(toCity); getDestinations(fromCity).add(toCity); } /*
  • Defines a city if it has not already been defined. Defining
  • a city consists of entering it in the cities array and
  • entering an empty ArrayList in the flights table to show
  • that it has no destinations yet. / private void defineCity(String cityName) { if (!cities.contains(cityName)) { cities.add(cityName); flights.put(cityName, new ArrayList()); } } / Private instance variables */ private HashMap<String,ArrayList> flights; private ArrayList cities; }