Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Find out the \"true\" MPG (Miles Per Gallon) of a vehicle, given a gr

ID: 3843477 • Letter: O

Question

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 printin0 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 beginning and final MPG. The following criteria will be used for the six inputs. 1. 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): U 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 switch0 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, etc.

Explanation / Answer

As the language is not specified, I will use that liberty to write the code in java. Here it is:

import java.util.Scanner;
class MPG {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double MPG1,MPG2;
boolean road;
String weather;
char elevation;
double weight;
int speed;
boolean engine;

System.out.println("Please Enter MPG: ");
MPG1 = input.nextDouble();
MPG2=MPG1;
System.out.println("Are you driving in the city or on the highway? ");
System.out.println("Enter true for city and false for highway: ");
road = input.nextBoolean();
System.out.println("Is it rainy or sunny? ");
System.out.println("Enter "Rainy" for rainy and "Sunny" for sunny: ");
weather = input.next();
System.out.println("What is the elevation? ");
System.out.println("Enter 'S' for Steep 'H' for Hilly 'F' for Flat: ");
elevation = input.next().charAt(0);
System.out.println("What is the weight of the vehicle(in lbs)? ");
weight = input.nextDouble();
System.out.println("What is the speed you prefer? ");
System.out.println("1 for under speed limit");
System.out.println("2 for at speed limit");
System.out.println("3 for 5% above speed limit");
System.out.println("4 for 10% above speed limit");
System.out.println("5 for 20% above speed limit");
speed = input.nextInt();
System.out.println("Is your engine old or new? ");
System.out.println("Enter true for old and false for new: ");
engine = input.nextBoolean();
if(road==true)MPG2-=2;
if(road==false)MPG2+=5;
if(weather=="Rainy")MPG2-=1;
if(elevation=='S')MPG2-=5;
if(elevation=='H')MPG2-=3;
while(weight>=100){
   MPG2-=0.5;
   weight/=100;
}
switch(speed){
   case 1: MPG2+=2; break;
   case 2: break;
   case 3: MPG2-=1; break;
   case 4: MPG2-=3; break;
   case 5: MPG2-=5; break;
}
if(engine==true)MPG2-=2;
System.out.println("Beginning MPG: " + MPG1 + " Final MPG:" + MPG2);

}
}

Let me know if you encounter and problem !