Create two Java files. One file will contain the main() method. Second file will
ID: 3705840 • Letter: C
Question
Create two Java files.
One file will contain the main() method.
Second file will hold all the information of a class called "Auto_Usage".
In the main method:
A. This method shall interact with the user.
B. It shall contain a menu with 4 items to choose.
C. The main() must declare an instance of the "Auto_Usage" class
These are the menu option:
1. "Add miles driven to the car" - Ask user to enter the miles driven on the last trip. Call the "AddMiles()" in the "Auto_Usage" class.
2. "Add gallons of gas pumped into the car" - Ask user to enter both gallons pumped and money spent for the gas. Call the "AddGas()" method in the "Auto_Usage" class.
3. Calculate the Miles/Gallons
4. Exit the Program
Allow the User to keep entering data until they coose option 4.
In the "Auto_Usage" Class:
A. Should have field variables: totalMiles, totalGallons, totalGasMoney
B. Create a constructor() method that initializes all of the field variables to 0.
C. The methods below are not static methods.
1. Public void AddMiles(double newMiles) method - This will keep a total of all the miles driven on this vehicle.
2. Public void AddGas(double newGallons, double moneySpent) - This method will keep a total of both gallons used and money spent of gas.
3. Public double Calc_MPG() - Using the data stored in the fields of the class, calculate and return the value.
Explanation / Answer
Auto_Usage_Test.java
import java.util.Scanner;
public class Auto_Usage_Test {
public static void main(String[] args) {
Auto_Usage a = new Auto_Usage();
Scanner scan = new Scanner(System.in);
System.out.println("Menu: 1.Add miles driven to the car 2.Add gallons of gas pumped into the car 3.Calculate the Miles/Gallons 4.Exit Enter you choice:");
int choice = scan.nextInt();
while(choice!=4) {
if(choice==1) {
System.out.println("Enter the miles driven on the last trip: ");
double miles = scan.nextDouble();
a.AddMiles(miles);
} else if(choice==2) {
System.out.println("Enter both gallons pumped and money spent for the gas: ");
double gallons = scan.nextDouble();
double money = scan.nextDouble();
a.AddGas(gallons, money);
} else if(choice == 3) {
System.out.println("MEPG:");
System.out.println(a.Calc_MPG());
}
System.out.println("Menu: 1.Add miles driven to the car 2.Add gallons of gas pumped into the car 3.Calculate the Miles/Gallons 4.Exit Enter you choice:");
choice = scan.nextInt();
}
}
}
Auto_Usage.java
public class Auto_Usage {
private double totalMiles, totalGallons, totalGasMoney;
public Auto_Usage() {
totalMiles=0;
totalGallons=0;
totalGasMoney=0;
}
public void AddMiles(double newMiles) {
totalMiles+=newMiles;
}
public void AddGas(double newGallons, double moneySpent) {
totalGallons+=newGallons;
totalGasMoney+=moneySpent;
}
public double Calc_MPG() {
return totalMiles * totalGallons;
}
}
Output:
Menu:
1.Add miles driven to the car
2.Add gallons of gas pumped into the car
3.Calculate the Miles/Gallons
4.Exit
Enter you choice:
1
Enter the miles driven on the last trip:
450
Menu:
1.Add miles driven to the car
2.Add gallons of gas pumped into the car
3.Calculate the Miles/Gallons
4.Exit
Enter you choice:
2
Enter both gallons pumped and money spent for the gas:
120
123.5
Menu:
1.Add miles driven to the car
2.Add gallons of gas pumped into the car
3.Calculate the Miles/Gallons
4.Exit
Enter you choice:
3
MEPG:
54000.0
Menu:
1.Add miles driven to the car
2.Add gallons of gas pumped into the car
3.Calculate the Miles/Gallons
4.Exit
Enter you choice:
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.