Stack Flight



Stack Flight
Help with Java program. Adjacency lists, stacks, and searches?

I’m given 3 text files

cityFile – contains all of the cities
flightFile – contains a source city, destination city, and flight number separated by commas
requestFile – which is the request to fly from a source city to a destination city.

The program I write must use the input from the request file, along with adjacency lists and an implementation of stacks, to search for and output the flight path to the destination city.

So far I’ve only read in the cityFile to an array called cityList.

How should I proceed in writing the program from here?

Well, my suggestion would be.

Create a City class that contains just the name of the city.

public class City
{
private String name;

public City(String tbeName)
{
name = theName;
}

public String getName()
{
return name;
}
}

Then create a Flight class.

public class Flight
{
private City source;
private City destination;
private String flightNumber;

public Flight(City source, City destination, String number)
{
this.source = source;
this.destination = destination;
flightNumber = number;
}

// Have get methods for the source, destination and flight number.
// Have a set method to change the fligh number if you want.
}

Now that you have these two classes, you can use them in your program.

Make another class that will read in the files.

Make an array of City classes that will hold the cities.
City cityArray[] = new City[ ];
Then read in the cities and make a new City for each one and ut it in the array.

Create an array of Flight classes that hold the flights.
Flight flightArray[] = new Flight[ ];

Now, read in the flight file and use a StringTokenizer to separate the items in the flight file.
StringTokenizer tokenizer;
String sourceCityString;
String destinationCityString;
String flightNumber;
City sourceCity;
City destinationCity;
Flight theFlight;
You can change the names of these if you want.

Read the lines of this file in the same way you do with the cities file.
For each line, create a StringTokenizer with the line.

tokenizer = new StringTokenize( , “,”);

Then as you know each line has 3 elements, do this.
sourceCityString = tokenizer.nextToken();
destinationCityString = tokenizer.nextToken();
flightNumber = tokenizer.nextToken();

Now using this information, find the city in the array that is the city in the string.

for (int i = 0; i < cityArray.length; i++)
{
if (sourceCityString.equals( cityArray[ i ].getName() ))
{
sourceCity = cityArray[i];
break; // To stop the loop.
}
}

Do the same thing for the destination city.

Then create a new Flight class using this information and put it in the flights array. You will need to keep an int for the index of the next available place in the array.

Then after you have created the cities and flights array, you can read in the request file.

You will probably need to use a StringTokenizer again to get the information from the lines in the file.

Then using the information, check each item in the flights array to see if the source city name is the same as the source in the request city. if it is, you then need to check if the destantion city is correct. if it is, you then have the correct flight. If not, you will have to keep looping through the array to find the correct flight.

Good luck with it.

Joe Stack & Flight 253 News Update


This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

Comments are closed.