Java: Write a program to allow a user to enter the name of a starting city and a
ID: 3607317 • Letter: J
Question
Java: 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 fuel stations on the way. The number of fuel stations between two cities is the sum of the number of fuel stations in the starting city and the number of fuel stations in the destination city, all divided by 4 (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 fuel stations in each city in a 1D 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 fuel stations are in each city.
For example, if Houston has 100 fuel stations and Beaumont has 20, then the output of your program should look something like:
City Mileage w/ Fuel 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: 75 miles
Estimated # of fuel stations: 30
The pseudocode for this HW from our professor looks like this:
Explanation / Answer
import java.io.*;
import java.util.*;
public class DemoCity{
public static void main(String args[]){
String[] cities = new String[8];
int[] stations = new int[8];
int[][] mileage = new int[8][8];
Scanner sc = new Scanner(System.in);
System.out.println("City Mileage w/ Fuel Stations ver. 1.0 (C) 2017 First Last");
cities[0] = "Dallas";
cities[1] = "Houston";
cities[2] = "Beaumont";
cities[3] = "Austin";
cities[4] = "El Paso ";
cities[5] = "Lubbock";
cities[6] = "Texarkana";
cities[7] = "Corpus Christi";
stations[0] = 85;
stations[1] = 100;
stations[2] = 20;
stations[3] = 75;
stations[4] = 44;
stations[5] = 59;
stations[6] = 37;
stations[7] = 39;
for (int i = 0; i<8; i++){
for (int j = 0; j<8; j++){
mileage[i][j] = 0; // Can be hard coded or can be taken from the user as input with sc.nextInt()
}
}
System.out.println("The cities in the database are:");
for (int i = 0; i<8; i++){
System.out.println(i+1 + "." + cities[i] + "(" + stations[i]+ ")");
}
System.out.println("Enter the starting city :");
String startCity = sc.next();
System.out.println("Enter the destination city:");
String endCity = sc.next();
int startindex = 0;
int endindex = 0;
for (int i = 0; i<8; i++){
if (cities[i].equals(startCity))
startindex = i;
if (cities[i].equals(endCity))
endindex = i;
}
int num_fuel_stations = (stations[startindex] + stations[endindex])/4;
System.out.println("Mileage:" + mileage[startindex][endindex]);
System.out.println("Estimated # of fuel stations:" + num_fuel_stations);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.