Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2. You can represent a route map in a Java program with two arrays. The

ID: 3778241 • Letter: E

Question

Exercise 2. 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.xx

Test your program with this data and with two other sets of data different than above (change the array data).

Exercise 2. 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 I 1 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 the 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 a 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 200 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.xx Test your program with this data and with two other sets of data different than above (change the array data)

Explanation / Answer

Please follow the code and comments for description :  

CODE :

import java.text.DecimalFormat; // required imports

public class CostCalc { // class to run the code

public static void main(String[] args) { // driver method

String[] city = {"Halifax", "Moncton", "Fredericton", "Quebec City", "Montreal", "Toronto"}; // required arrays
int[] distance = {260, 180, 520, 250, 504, 0};
double[] price = {1.1, 1.0, 1.15, 0.98, 1.01, 0};
int carMileage = 13; // local variables
double costPerLoc, totalCost = 0, quantPerLoc = 0, totalQuant = 0, remGas = 0;

DecimalFormat f = new DecimalFormat("##.00"); // round to two decimals

System.out.println("Buy Gas as Follows : "); // message
for (int i = 0; i < 6; i++) { // iterate over the array size

if (i > 0 && i < 4) { // check for the initial value
if (price[i] < price[i + 1]) { // check for the current and the future price
quantPerLoc = 40; // set the limit to car capacity
System.out.print("At " + city[i] + " : Buy " + f.format(quantPerLoc) + " litres, and Pay $"); // message
costPerLoc = price[i] * 40; // calculate the cost
System.out.print(f.format(costPerLoc) + ". "); // print the data
} else {
System.out.print("At " + city[i] + " : Buy "); // message
quantPerLoc = distance[i] / carMileage; // calculate the distance to get the quantity
quantPerLoc = quantPerLoc - remGas; // subtract from the remaining gas left
System.out.print(f.format(quantPerLoc) + " litres, and Pay $"); // message
costPerLoc = price[i] * quantPerLoc; // calculate the cost
System.out.print(f.format(costPerLoc) + ". "); // print data to console
}
} else {
System.out.print("At " + city[i] + " : Buy "); // for the initial and final conditions
if (i == 5) { // check for the last value
quantPerLoc = 0; // set the values
System.out.print(quantPerLoc + " litres, and Pay $"); // message
costPerLoc = price[i] * quantPerLoc; // calculate the cost
System.out.print(costPerLoc + ". "); // print
} else {
quantPerLoc = distance[i] / carMileage; // calculate and subtract form the remaining gas
quantPerLoc = quantPerLoc - remGas;
System.out.print(f.format(quantPerLoc) + " litres, and Pay $"); // message
costPerLoc = price[i] * quantPerLoc; // get the cost
System.out.print(f.format(costPerLoc) + ". "); // message
}
}
remGas = distance[i] / 13; // get the remaining gas left
if (remGas != quantPerLoc) {
remGas = quantPerLoc - remGas;
}
totalCost = totalCost + costPerLoc; // sum up the total cost and the quantity
totalQuant = totalQuant + quantPerLoc;
}
System.out.print("You Purchased " + totalQuant + " litres of Gas and the Total Cost Would be $" + f.format(totalCost) + " "); // print data to console
}
}


OUTPUT :

Buy Gas as Follows :
At Halifax : Buy 20.00 litres, and Pay $22.00.
At Moncton : Buy 40.00 litres, and Pay $40.00.
At Fredericton : Buy 13.00 litres, and Pay $14.95.
At Quebec City : Buy 40.00 litres, and Pay $39.20.
At Montreal : Buy 17.00 litres, and Pay $17.17.
At Toronto : Buy 0.0 litres, and Pay $0.0.
You Purchased 130.0 litres of Gas and the Total Cost Would be $133.32


Hope this is helpful.