Write a program to allow a user to enter the name of a starting city and a desti
ID: 3810670 • Letter: W
Question
Write a program to allow a user to enter the name of a starting city and a destination city. Output the mileage between cities and an estimate of the total number of gas stations on the way. The number of gas stations between two cities Is the sum of the number of gas stations In the starting city and the number of gas stations in the destination city, all divided by 3 (since many of the stations will likely not be on the road between the two cities). Store the mileage between 8 different cities in a 2D array of integers. Choose names of cities in Texas. Store the number of gas stations in each city in a 10 array. Your program should first print a title showing the name of the program, version number and copyright notice with author name (insert your name as the author). Also, print a list of the cities in the database so the user knows what cities they can search. Look up the mileage between the cities on the Internet and use the values you find in your program. You are free to create your own estimate of how many gas stations are in each city. For example, if Houston has 100 gas stations and Beaumont has 20, then the output of your program should look something like. City Mileage w/Gas Stations ver. 1.0 (C) 20017 First Last The cities ir. the database are: 1. Dallas (85 stations) 2. Houston (100 stations) 3. Beaumont (20 stations) 4. Austin (75 stations) 5. SI Paso (44 stations) 6. Lubbock (59 stations) 7. Texarkana (37 stations) 8. Corpus Christi (39 stations) Enter the starting city: Beaumont Enter the destination c;ty: Houston Mileage: 75 miles Estimated # of gas stations: 40 Put your name, the class (COSC 1336) and the title of the assignment in comments at the top of the program.Explanation / Answer
GasStations.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GasStations {
public static void main(String[] args) {
System.out.println("City Mileage w/ Gas Stations ver. 1.0 (C) 2017 First Last");
List<String> cities = new ArrayList<String>();
cities.add("Dallas");
cities.add("Houston");
cities.add("Beaumont");
cities.add("Austin");
cities.add("El Paso");
cities.add("Lubbock");
cities.add("Texarkana");
cities.add("Corpus Christi");
List<Integer> gasStations = new ArrayList<Integer>();
gasStations.add(85);
gasStations.add(100);
gasStations.add(20);
gasStations.add(75);
gasStations.add(44);
gasStations.add(59);
gasStations.add(37);
gasStations.add(39);
int mileage[][] = new int[8][7];
mileage[0][0] = 239;
mileage[0][1] = 286;
mileage[0][2] = 195;
mileage[0][3] = 635;
mileage[0][4] = 346;
mileage[0][5] = 179;
mileage[0][6] = 411;
mileage[1][0] = 239;
mileage[1][1] = 85;
mileage[1][2] = 165;
mileage[1][3] = 744;
mileage[1][4] = 522;
mileage[1][5] = 290;
mileage[1][6] = 207;
mileage[2][0] = 286;
mileage[2][1] = 85;
mileage[2][2] = 248;
mileage[2][3] = 827;
mileage[2][4] = 619;
mileage[2][5] = 261;
mileage[2][6] = 291;
mileage[3][0] = 195;
mileage[3][1] = 165;
mileage[3][2] = 248;
mileage[3][3] = 576;
mileage[3][4] = 373;
mileage[3][5] = 374;
mileage[3][6] = 217;
mileage[4][0] = 635;
mileage[4][1] = 746;
mileage[4][2] = 829;
mileage[4][3] = 576;
mileage[4][4] = 346;
mileage[4][5] = 813;
mileage[4][6] = 695;
mileage[5][0] = 346;
mileage[5][1] = 522;
mileage[5][2] = 619;
mileage[5][3] = 373;
mileage[5][4] = 346;
mileage[5][5] = 524;
mileage[5][6] = 529;
mileage[6][0] = 179;
mileage[6][1] = 290;
mileage[6][2] = 261;
mileage[6][3] = 374;
mileage[6][4] = 813;
mileage[6][5] = 524;
mileage[6][6] = 496;
mileage[7][0] = 411;
mileage[7][1] = 207;
mileage[7][2] = 291;
mileage[7][3] = 217;
mileage[7][4] = 695;
mileage[7][5] = 529;
mileage[7][6] = 496;
System.out.println("The cities in the database are:");
for(int i=0;i<cities.size();i++){
System.out.println((i+1)+". "+cities.get(i)+" ("+gasStations.get(i)+" stations)");
}
System.out.println(" ");
Scanner input = new Scanner(System.in);
System.out.print("Enter the starting city: ");
String startCity = input.next();
System.out.print("Enter the destination city: ");
String destCity = input.next();
int startIndex = cities.indexOf(startCity);
int destIndex = cities.indexOf(destCity);
System.out.println("Mileage: "+mileage[startIndex][destIndex]+" miles");
int noOfGasStations = (gasStations.get(startIndex)+gasStations.get(destIndex))/3;
System.out.println("Estimated # of gas stations: "+noOfGasStations);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
Output
City Mileage w/ Gas Stations ver. 1.0 (C) 2017 First Last
The cities in the database are:
1. Dallas (85 stations)
2. Houston (100 stations)
3. Beaumont (20 stations)
4. Austin (75 stations)
5. El Paso (44 stations)
6. Lubbock (59 stations)
7. Texarkana (37 stations)
8. Corpus Christi (39 stations)
Enter the starting city: Beaumont
Enter the destination city: Houston
Mileage: 85 miles
Estimated # of gas stations: 40
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.