Need help with my JAVA hw, ASAP, ty. You can represent a route map in a Java pro
ID: 3571179 • Letter: N
Question
Need help with my JAVA hw, ASAP, ty.
You can represent a route map in a Java program with two arrays. The first array can contain the names of the towns on the route and the second can contain the distances between the towns. For example, you could represent the route from Halifax to Toronto with the following two arrays: String[] city = {"Halifax", "Moncton", "Fredericton", "Quebec City", "Montreal", "Toronto"} int[] distance = {260, 180, 520, 250, 504, 0}; As well a third array can hold the price of gas per litre in each city. double [] price = {1.1, 1.0, 1.15, 0.98, 1.01, 0} Assume that your car holds 40 L of gas and the car drives 13 kilometers per litre (KMPL). Based on these arrays, to go from Halifax to Moncton it is 260 km. To go 260 km, you need 20 L of gas (distance/KMPL). The price of gas in Halifax is $1.10/L and the price of gas in Moncton is $1.00/L. Write a program that will work out how much gas (in Litres) you should buy at each city to pay the least when traveling from Halifax to Toronto (assume that you only know the price of gas at the current city and the next city you are travelling to). Note, you do not need to use methods for this question (that is, you can write the program all in the main method). For the above data the program would print (you need to deal with decimals places - maximum 2 decimal places): Buy gas as follows: Halifax: Buy 20 litres Pay $22.00 Moncton: Buy 40 litres Pay $40.00 Fredericton: Buy XX litres Pay $xx.xx Quebec City: Buy XX litres Pay $xx.xx Montreal: Buy XX litres Pay $xx.xx Toronto = Buy XX litres Pay $xx.xx You purchased X Litres and the Total Cost would be $ xx.xxExplanation / Answer
String city[]={ };
int distance={260,180,520,250,504,0};
int regas[5];
int totalprice[5];
int price={1.1,1.10,1.15,0.98,1.01,0};
for(int i=0;i<5;i++)
{regas[i]=distance[i]/13;
System.out.println("the gas needed is"",+regas[i]);
totalprice[i]=regas[i]*price[i];
System.out.println("the price is here""+totalprice[i]);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.