Java Application - DistanceCalculator

profileTopsolutions
 (Not rated)
 (Not rated)
Chat

 

Java Application - DistanceCalculator

 

Create a Java application named DistanceCalculator1.java 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 (the text file is attached in the folder):

 

 
 

7

Chicago,0,983,787,714,1375,967,1087

Boston,983,0,214,1102,1763,1723,1842

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

Atlanta,714,1102,888,0,661,781,810

Miami,1375,1763,1549,661,0,1426,1187

Dallas,967,1723,1548,781,1426,0,239

Houston,1087,1842,1627,810,1187,239,0

 

 

 

 

 

 

 

 

 

 

 

 

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.

 

The 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:  DistanceCalculator1

 

Add descriptive comments throughout the program.

 

    • 9 years ago
    Complete A++ Solution
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      58cgfr42.zip