Write code for the application that allows users to calculate the expected cost
ID: 3937848 • Letter: W
Question
Write code for the application that allows users to calculate the expected cost of a trip as follows:
User can enter anticipated miles
User can enter anticipated cost of gas per gallon
The application should:
Provide interface options to allow the user to calculate anticipated cost and exit the application
Calculate and display the approximate cost, including oil change
Write code for the application that allows users to calculate the expected cost of a trip as follows:
User can enter anticipated miles
User can enter anticipated cost of gas per gallon
The application should:
Provide interface options to allow the user to calculate anticipated cost and exit the application
Calculate and display the approximate cost, including oil change
Explanation / Answer
Write code for the application that allows users to calculate the expected cost of a trip as follows:
User can enter anticipated miles
User can enter anticipated cost of gas per gallon
The application should:
Provide interface options to allow the user to calculate anticipated cost and exit the application
Calculate and display the approximate cost, including oil change
Answer :-
import java.util.Scanner;
public class GasCalculator {
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("How many miles do you plan to travel?");
int miles = scan.nextInt();
System.out.println("So you will drive " + miles +" miles.");
System.out.println("What is the price of gas?");
double gas = scan.nextDouble();
System.out.println("Ok, so gas costs $" + gas +" per gallon.");
System.out.println("What is the average MPG of your car? Use the nearest whole number.");
int mpg = scan.nextInt();
System.out.println("So, you get " + mpg +" miles per gallon.");
System.out.println("Would you like an oil change? Enter Y or N");
double cost = (miles / mpg * gas + oil);
System.out.println("Below is your total cost");
System.out.println(String.format("Your trip will cost $" + cost + "."));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.