Java Application - DistanceCalculator

Java Application - DistanceCalculator

Java Application - DistanceCalculator

Create a Java application named to solve the following problem. 

An application is needed to allow a user to enter two cities and display the distances between them.

A text file named CityDistances.txt contains the names of cities and the distances between each city.  The file has the following contents: 




New York,787,214,0,888,1549,1548,1627





Notice that the file tokens are delimited by commas.

The first number in the file is the number of cities.  The next lines contain the city names and the distances between cities that can be used to create a two dimension array to model the distance table shown on the previous page. 

Your application should read the cities into a one dimensional array and the distances into a two dimensional array.

After the arrays are created, display the number of cities and a list of cities and then prompt the user to enter two cities and then display the distances between the two cities.  Validate the user input to not allow invalid cities to be entered.  Create a loop to continue prompting until the user wants to exit the program. 

Use fields to hold the city names array and the distances array. 

Use modular programming.  I suggest a method to load the arrays from the file, and a method to retrieve the city numbers and display the distance. 

Program output might look like this:

Number of cities 7

City number 1 Chicago

City number 2 Boston

City number 3 New York

City number 4 Atlanta

City number 5 Miami

City number 6 Dallas

City number 7 Houston


Distance from city number: 2

To city number: 5

Boston to Miami 1763 miles

Continue (yes/no)yes 

Distance from city number: 1

To city number: 7
Chicago to Houston 1087 miles

Continue (yes/no)no

Name the Java Class:  DistanceCalculator
Powered by