Question

Objective: Find out the “true” MPG (Miles Per Gallon) of a vehicle, given a group of...

Objective: Find out the “true” MPG (Miles Per Gallon) of a vehicle, given a group of conditions which the user provides values for. 5 are given, 1 you create on your own. Requirements: Prompt the user for the car’s MPG first. i.e. Please Enter MPG: 26 Then prompt the user to enter a value for six factors which will affect the MPG of the vehicle, prompting for single value inputs after showing menus (i.e. a series of println() statements showing the options/valid values to enter) After collecting all of the inputs, provide a detailed report of the inputs, their meaning, how much they affect MPG, as well as the the beginning and final MPG. The following criteria will be used for the six inputs. City or Highway Driving: Use a boolean type variable for input City Subtracts 2 from MPG Highway Adds 5 to MPG Rainy or Sunny: Use a String type variable for input “Rainy” Subtracts 1 from MPG “Sunny” No Change to MPG Elevation: Use a char type variable for input ‘S’ for Steep, Steep Subtracts from 5MPG ‘H’ for Hilly, Hilly Subtracts 3 from MPG ‘F’ for Flat, No Change to MPG Weight in Vehicle (cargo or passenger): Use double type for input Each 100lbs subtracts 0.5 MPG Speed Preference: Use an int type variable for input 1 for under speed limit, Add 2 to MPG 2 for at speed limit, No Change to MPG 3 for 5% above speed limit, Subtract 1 from MPG 4 for 10% above speed limit, Subtract 3 from MPG 5 for over 20% above speed limit, Subtract 5 from MPG Suggestion: Use a switch() statement for Speed Preference Add one additional factor which you will prompt the user for, that affects MPG such as tire pressure/condition, hybrid vs. gas powered, engine condition, towing load, 2WD/4WD,

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

import java.util.*;
//MPG Calculater class
class MPGCalculater{
   Scanner sc;//scanner class
   double mpg;//mpg
   int option;
   public MPGCalculater(){
       sc=new Scanner(System.in);
       System.out.println("Enter intial mpg::");
       mpg=sc.nextDouble();
       System.out.println("MPG::"+mpg);
       //doing job
       while(true){
           menu(); //displaying menu
           System.out.printf("Enter ur option::");
           option=sc.nextInt();//taking user selection
          
           if(option==0){
               break;//stoping programe
           }
           //chosing apprpriate function based on users choice
           switch(option){
               case 1:
                   CityOrDriving();
                   break;
               case 2:
                   RainySunny();
                   break;
               case 3:
                   Elevation();
                   break;
               case 4:
                   weightInVenhicle();
                   break;
               case 5:
                   speed();
                   break;
               case 6:
                   towingload();
                   break;
               default:
                   System.out.println("Invalid option");
                   break;
           }
       }
      
   }
   //menu
   public void menu(){
       System.out.println("Choose");
       System.out.println("1->City or Highway Driving");
       System.out.println("2->Rainy or Sunny:");
       System.out.println("3->Elevation");
       System.out.println("4->Weight in Vehicle (cargo or passenger)");
       System.out.println("5->Speed Preference");
       System.out.println("6->towing load");
       System.out.println("0->Stop");
      
   }
   //substract mpg
   public void substract(double ammount){
       System.out.println("MPG::"+mpg);
       mpg=mpg-ammount;
       System.out.println("MPG::"+mpg);
   }
       //substract mpg
   public void add(double ammount){
       System.out.println("MPG::"+mpg);
       mpg=mpg+ammount;
       System.out.println("MPG::"+mpg);
   }

   public void CityOrDriving(){
       System.out.println("Enter \ntrue->City \nfalse->Driving");
       System.out.printf("Enter your option::");
       boolean option=sc.nextBoolean();
       //City Subtracts 2 from MPG
       if(option){
           substract(2);
       }
       //Highway Adds 5 to MPG
       else{
           add(5);
       }

   }
   //Rainy or Sunny
   public void RainySunny(){
       System.out.println("Enter Rainy or sunny");
       System.out.printf("Enter your option::");
       String option=sc.next();
      
       //Rainy Subtracts 1 from MPG
       if(option.toLowerCase().equals("rainy")){
           substract(2);
       }
       //Sunny No Change to MPG
       else if(option.toLowerCase().equals("sunny")){
           add(0);
       }
       else{
           System.out.println("Invalid option");
       }
   }
   //Elevation
   public void Elevation(){
       System.out.println("S for Steep, Steep Subtracts from 5MPG");
       System.out.println("H for Hilly, Hilly Subtracts 3 from MPG");
       System.out.println("F for Flat, No Change to MPG");
       System.out.printf("Enter your option::");
       char c=sc.next().toLowerCase().charAt(0);
       switch(c){
       //S for Steep, Steep Subtracts from 5MPG
           case 's':
               substract(5);
               break;
       //H for Hilly, Hilly Subtracts 3 from MPG
           case 'h':
               substract(3);
               break;
       //F for Flat, No Change to MPG
           case 'f':
               add(0);
               break;
           default:
               System.out.println("Invalid option");
       }
   }
   //Weight in Vehicle
   public void weightInVenhicle(){
       System.out.println();
       System.out.println("Each 100lbs subtracts 0.5 MPG");
       System.out.printf("Enter weight::");
       double weight=sc.nextDouble();
       substract((weight/100)*0.5);
   }
   //SPEED PERFORANCE
   public void speed(){
      
       String menu="1 for under speed limit, Add 2 to MPG\n"+
                   "2 for at speed limit, No Change to MPG\n"+
                   "3 for 5% above speed limit, Subtract 1 from MPG\n"+
                   "4 for 10% above speed limit, Subtract 3 from MPG\n"+
                   "5 for over 20% above speed limit, Subtract 5 from MPG\n";
       System.out.println(menu);
       System.out.printf("Enter your option::");
       int option=sc.nextInt();
       switch(option){
          
       //1 for under speed limit, Add 2 to MPG
           case 1:
               add(2);
               break;
       //2 for at speed limit, No Change to MPG
           case 2:
               add(0);
               break;
       //3 for 5% above speed limit, Subtract 1 from MPG
           case 3:
               substract(1);
               break;
       //4 for 10% above speed limit, Subtract 3 from MPG
           case 4:
               substract(3);
               break;
       //5 for over 20% above speed limit, Subtract 5 from MPG      
           case 5:
               substract(5);
               break;
           default:
               System.out.println("Invalid option");
               break;
       }  
   }
   public void towingload(){
       //sustract 5 from mpg
       substract(5);
   }
   public static void main(String args[]){
       new MPGCalculater();
   }
}

6 PG::26.0 hoose -City or Highway Driving Rainy or Sunny Elevation Weight in Uehicle <cargo or passenger> -Speed Preference t
Add a comment
Know the answer?
Add Answer to:
Objective: Find out the “true” MPG (Miles Per Gallon) of a vehicle, given a group of...
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
  • 8.2.19-T The table below contains the overall miles per gallon (MPG) of a type of vehicle...

    8.2.19-T The table below contains the overall miles per gallon (MPG) of a type of vehicle Complete parts a and b below 24 24 20 26 24 29 35 23 28 20 44 31 25 22 a. Construct a 95% confidence interval estimate for the population mean MPG for this type of vehicle, assuming a normal distribution The 95% confidence interval estimate is from (Round to one decimal place as needed) MPG to MPG 8.EOC.57-E Question Help A consulting firm...

  • Compute for Miles Per Gallon in C++

    Make a program that will calculate and compute for the quotient of miles and gallons (mpg: miles per gallons). Your program should must ask the user to specify the size of the array (using dynamic array) for the following variable: miles ,gallons and mpg. Prompt the user to Initialize  the value of miles (value for miles should be 100-250) and gallons (values should be from 5-25). Use pointer galPtr for gallons, milPtr for miles and mpgPtr for mpg.  Use function...

  • You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

    You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Messages commenting on a vehicle's fuel efficiency And also validates input Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove...

  • CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle cla...

    CIS247C Week 3 Project Overview The objective of this week is to enhance last week's Vehicle class by making the following changes: • Create a static variable called numVehicles that holds an int and initialize it to zero. This will allow us to count all the Vehicle objects created in the main class. • Add the copy constructor • Increment numVehicles in all of the constructors • Decrement numVehicle in destructor • Add overloaded versions of setYear and setMpg that...

  • Car engine displacement in liters and fuel consumption for combined city/highway in miles per gallon (MPG)....

    Car engine displacement in liters and fuel consumption for combined city/highway in miles per gallon (MPG). This data is from 2015 Fuel Economy Guide for the Department of Energy (DOE) Engine a. Make a scatterplot of the car MPG by displacement from the data in columns B and C. Displacement Combined Be sure the scatterplot contains the 5 important points of a chart or graph. Car Liters MPG Acura RLX 3.5 22.0 b. Place the regression trend line on the...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...

    This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total Also I would need a new name for the daubles. An example here might be double tRtalD-caradd xD, YD) 1. (a) We are going to begin with some simple math, reading input, and formatted output. Create a class called MuCalsustec Yau will not have an instance variable. Since you have no instance variables to initialize, you do nat need a constructar. Do NOT create...

  • Programming language: PYTHON Prompt: You will be creating a program to show final investment amounts from...

    Programming language: PYTHON Prompt: You will be creating a program to show final investment amounts from a principal using simple or compound interest. First, get a principal amount from the user. Next, ask the user if simple or compound interest should be used. If the user types in anything other than these options, ask again until a correct option is chosen. Ask the user to type in the interest rate as a percentage from 0 to 100%. Finally, ask the...

  • In June 2008, when gasoline prices were at an all-time high (more than $3.81 per gallon),...

    In June 2008, when gasoline prices were at an all-time high (more than $3.81 per gallon), Chrysler Motor Company promoted its Jeep vehicle with the offer of either $4,250 off the price of the vehicle or the guarantee that the buyer would not pay more than $2.75 per gallon of gas for the next 3 years (the details of the guarantee could vary by dealer). Required: 1. Assume that the Jeep vehicle you are interested in gets 15 mpg combined...

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