Question

import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

import java.util.Scanner;

public class MPGMain
{

   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
      
       Mileage mileage = new Mileage();
      
       System.out.println("Enter your miles: ");
       mileage.setMiles(input.nextDouble());
      
       System.out.println("Enter your gallons: ");
       mileage.setGallons(input.nextDouble());
      
       System.out.printf("MPG : %.2f",mileage.getMPG());
      
   }

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

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 user’s 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)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*The java program that prompts user to enter a menu choice . The program checks if user input is valid or not. If inpu is valid the prompt for the corresponding the input values and find the results otherwise print the message INVALID INPUT . The program continues till user enters 4 to exit the program.
* */


//Main.java
import java.util.Scanner;
public class Main
{
   private static Scanner input = new Scanner(System.in);
   public static void main(String[] args)
   {

       int choice;
       boolean run=true;

       //Declare variables of Mileage,Distance and Speed class
       Mileage mileage =null;
       Distance distance=null;
       Speed speed=null;
      
       /*Continue till user enters 4 to exit*/
       while(run)
       {
           //calling menu method
           choice=menu();
           switch (choice)
           {
           case 1:
               //Create an instanace of Mileage class
               mileage=new Mileage();
               System.out.println("Enter your miles: ");
               double miles=Double.parseDouble(input.nextLine());
               mileage.setMiles(miles);
              
               System.out.println("Enter your gallons: ");
               double gallons=Double.parseDouble(input.nextLine());
               mileage.setGallons(gallons);
              
               System.out.printf("MPG : %.2f",mileage.getMPG());
               break;
              
           case 2:
               //Create an instanace of Distance class
               distance=new Distance();
               System.out.println("Enter your speed in miles/hour: ");
               double inputspeed=Double.parseDouble(input.nextLine());
               distance.setSpeed(inputspeed);
              
               System.out.println("Enter your time in hours: ");
               double inputhours=Double.parseDouble(input.nextLine());
               distance.setTime(inputhours);
              
               System.out.printf("Distance : %.2f miles ",distance.getDistance());
               break;
              
           case 3:
               //Create an instanace of Speed class
               speed=new Speed();
               System.out.println("Enter your distance in miles: ");
               double inputmiles=Double.parseDouble(input.nextLine());
               speed.setDistance(inputmiles);
              
               System.out.println("Enter your time in hours: ");
               double inputtime=Double.parseDouble(input.nextLine());
               speed.setTime(inputtime);
               System.out.printf("Speed : %.2f miles/hours",speed.getSpeed());
               break;
              
           case 4:
               //exit from the program
               System.out.println("Bye!");
               run=false;
           }
       }
   }

   public static int menu()
   {
       int choice=0;
       do
       {
           System.out.println();
           System.out.println("Enter 1 to Determine Miles-per-Gallon ");
           System.out.println("Enter 2 to Determine Distance");
           System.out.println("Enter 3 to Determine Miles-per-Hour");
           System.out.println("Enter 4 to Exit");
           System.out.print("Enter your choice[1-4] : ");
           choice=Integer.parseInt(input.nextLine());
          
           if(choice<1 || choice>4)
               System.out.println("INVALID CHOICE");
          
       }while(choice<1 || choice>4);  
       return choice;   //return choice value
   } //end of the method ,menu
  
} //end of the class

Sample Output:


Enter 1 to Determine Miles-per-Gallon
Enter 2 to Determine Distance
Enter 3 to Determine Miles-per-Hour
Enter 4 to Exit
Enter your choice[1-4] : 5
INVALID CHOICE

Enter 1 to Determine Miles-per-Gallon
Enter 2 to Determine Distance
Enter 3 to Determine Miles-per-Hour
Enter 4 to Exit
Enter your choice[1-4] : 1
Enter your miles:
100
Enter your gallons:
10
MPG : 10.00
Enter 1 to Determine Miles-per-Gallon
Enter 2 to Determine Distance
Enter 3 to Determine Miles-per-Hour
Enter 4 to Exit
Enter your choice[1-4] : 2
Enter your speed in miles/hour:
10
Enter your time in hours:
10
Distance : 100.00 miles
Enter 1 to Determine Miles-per-Gallon
Enter 2 to Determine Distance
Enter 3 to Determine Miles-per-Hour
Enter 4 to Exit
Enter your choice[1-4] : 3
Enter your distance in miles:
10
Enter your time in hours:
5
Speed : 2.00 miles/hours
Enter 1 to Determine Miles-per-Gallon
Enter 2 to Determine Distance
Enter 3 to Determine Miles-per-Hour
Enter 4 to Exit
Enter your choice[1-4] : 4
Bye!

Add a comment
Know the answer?
Add Answer to:
import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables...

    import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //ask the user for a temperature System.out.println("Enter a temperature:"); double temp = scnr.nextDouble(); //ask the user for the scale of the temperature System.out.println("Is that Fahrenheit (F) or Celsius (C)?"); char choice = scnr.next().charAt(0); if(choice == 'F') { //convert to Celsius if given temperature was Fahrenheit System.out.println(temp + " degrees Fahrenheit is " + ((5.0/9) * (temp-32)) + " degrees Celsius"); } else {...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT