JAVA Write code for the application that allows users to cal
JAVA!!
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
Solution
package org.students;
import java.util.Scanner;
public class AnticipatedCost {
//Declaring constant
static final double OIL_CHANGE=20;
public static void main(String[] args) {
//Declaring variables
int no_of_miles,miles_per_gallon;
double cost_of_gas_per_gallon,approximate_cost;
int choice;
//Scanner class object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
//This loop continue to execute until user chooses option 2
do
{
//Displaying the menu
System.out.println(\"\ :: Menu ::\");
System.out.println(\"1.Calculate anticipated cost\");
System.out.println(\"2.Exit\");
//Getting the choice entered by the user
System.out.print(\"Enter Choice :\");
choice = sc.nextInt();
//Based on the choice the corresponding case will get executed.
switch (choice) {
case 1:
{
//Getting the no of miles entered by the user
System.out.print(\"Enter No of miles :\");
no_of_miles=sc.nextInt();
//Getting the no of miles per gallon entered by the user
System.out.print(\"Enter no of miles per gallon :\");
miles_per_gallon=sc.nextInt();
//Getting the cost of gas per gallon entered by the user
System.out.print(\"Enter cost of gas per gallon :$\");
cost_of_gas_per_gallon=sc.nextDouble();
//Calculating the approximate cost
approximate_cost=(no_of_miles/miles_per_gallon)*cost_of_gas_per_gallon+OIL_CHANGE;
//Displaying the approximate cost
System.out.println(\"Approximate cost :$\"+approximate_cost);
}
break;
case 2:
break;
}
}while(choice!=2);
}
}
________________________________
Output:
:: Menu ::
1.Calculate anticipated cost
2.Exit
Enter Choice :1
Enter No of miles :500
Enter no of miles per gallon :30
Enter cost of gas per gallon :$20
Approximate cost :$340.0
:: Menu ::
1.Calculate anticipated cost
2.Exit
Enter Choice :2
___________Thank You

