Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having a problem figuring out how to create this program in Java. I have the following code already. It will need 2 new classes

I'm having a problem figuring out how to create this program in Java. I have the following code already. It will need 2 new classes added into it called Distance and Speed. Any help would be appreciated.

**MPGMain class**

import java.util.Scanner;

public class MPGMain {

public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter miles driven: "); double miles = input.nextDouble(); System.out.println("Please enter gallons used: "); double gallons = input.nextDouble(); System.out.printf("Your MPG is %.2f", Mileage.getMPG(miles, gallons)); }

}

**Mileage class**

public class Mileage { private double miles; //miles variable private double gallons; //gallons variable public void setMiles(double miles) { this.miles = miles; } public void setGallons(double gallons) { this.gallons = gallons; } public double getMiles() { return miles; } public double getGallons() { return gallons; } public static double getMPG(double miles, double gallons) { double MPG = miles / gallons; return MPG; }

}

You will need to add two more classes to the Java Project. One called Distance and one called Speed. Similar to Mileage, Distance will have two private member variables, speed and time (both doubles); Speed will have two private member variables distance and time (again both are doubles). They will need sets and gets (also called accessors and mutators). Distance should have one more method called getDistance that performs the math calculation and returns the double totalDistance. Speed should have one more method called getMPH that performs the math calculation and returns the double mph Add a menu like the following to MPGMain: Enter 1 to Determine Miles-per-Gallon Enter 2 to Determine Distance Enter 3 to Determine Miles-per-Hour Enter 4 to Exit The program should switch on the users choice. The program should continue until the user enters 4. If the user enters a number greater than 4 or less than 1 the program should display an error message and prompt the user to reenter a number between 1 and 4. The program will continue to display the error message allowing the user to reenter their choice until they enter a number within the correct range. Each case in the switch statement should create an instance of the appropriate class (for example, case 1: Mileage, case 2: Distance, case 3: Speed), and set the variable values using the set methods of the class. Each case statement should also display the results of the appropriate mathematical calculations by calling the getMPG, getDistance, or getMPH methods.(This is my starting code plus setters and getters for distance and speed)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions

Question

The nature and importance of the global marketplace.

Answered: 1 week ago