Question

In: Computer Science

Print greeting Prompt the user for starting odometer reading Use a while loop to validate that...

Print greeting Prompt the user for starting odometer reading Use a while loop to validate that starting odometer reading is a positive number.

Initialize variables: last odometer reading, current odometer reading, leg number, total fuel, moreInput

While moreInput == ‘y’

Prompt the user for new odometer reading and fuel consumed If fuel is positive and new odometer reading > last odometer reading:

Calculate MPG for this leg using mpg = (new odometer – last odometer) / fuel Print MPG for this leg

Update last odometer reading, total fuel, leg number

Ask if user wants to continue (save user response in moreInput)

Else

Print message saying fuel should be positive and new odometer reading should be greater than last odometer reading

Print number of legs Calculate average MPG over entire journey (= Total miles travelled / total fuel consumed) Print average MPG over entire journey

Print Bye message.

Solutions

Expert Solution

hey there ! i am done with the code in java... Please give me a like to appreciate my work and efforts....

here is the code -


package MPG;

import java.util.Scanner;

/**
*
* @author Aditya kumar
*/
public class MPG {
public static void main(String args[])
{
//declaring variables
int lastodometer,currentodometer = 0,legnumber,totalfuel = 0;
char moreinput='y';
//for user input
Scanner sc=new Scanner(System.in);
//manually update the lastodometer
lastodometer=88;
legnumber=1;
double mpg;
//while loop
while(moreinput=='y')
{
//ask user to enter the values
System.out.println("Enter the newOdometer : ");
currentodometer=sc.nextInt();
System.out.println("Fuel consumed ? ");
totalfuel=sc.nextInt();
//check if totalfuel is positive and currentodometer is greater than lastodometer
if(totalfuel>=0 && currentodometer>lastodometer)
{
//calculate mpg
mpg=(currentodometer-lastodometer)/totalfuel;
System.out.println("The MPG : "+mpg);
//set the values
totalfuel=totalfuel;
legnumber++;
lastodometer++;
//ask user to continue
System.out.println("Do you wanna continue ? y/n ");
moreinput=sc.next().charAt(0);
}
else
{
//else part
System.out.println("fuel should be positive and new odometer reading should be greater than last odometer reading");

  
}
  
}
//calculate the average
double average=(currentodometer+lastodometer)/totalfuel;
System.out.println("average over entire journey : "+average);
  
System.out.println("Bye !");
  
  
}
}

and the snapshot of the output is -

Thank You ! Dont forget to like !


Related Solutions

Collect Customer Data - Part 2 Collect Mileage information: Prompt: "Starting Odometer Reading:\n" Variable: odoStart =...
Collect Customer Data - Part 2 Collect Mileage information: Prompt: "Starting Odometer Reading:\n" Variable: odoStart = ? Prompt: "Ending Odometer Reading:\n" Variable: odoEnd = ? Add code to PRINT odoStart and odoEnd variables as well as the totalMiles to check your work. The following data will be used as input in the test: Prompt the user to input the starting odometer reading (expect type int from the user) Prompt the user to input the ending odometer reading (expect type int...
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail.
. Use a validation loop to prompt for and get from the user a string that...
. Use a validation loop to prompt for and get from the user a string that is up to ten characters in length. Create value function encodedString that takes the string as input and returns the equivalent encoded string. Start with an empty encoded string. Use a for loop to encode each character of the input string. Use the following algorithm to encode a character and append the encoded character to the encoded string:
Write a do-while loop IN JAVA to build a Pokémon game Prompt the user for two...
Write a do-while loop IN JAVA to build a Pokémon game Prompt the user for two names one for their Pokémon, and one for the computers Pokémon Make two integer variables to hold the HP of each Pokémon, HP should be 20 for both Pokémon’s. Use Math.random() to determine the amount of damage caused, and which move the computer will select. The range of damage that can be done is up to you. Output to the user an attack menu...
Prompt the user to input their lucky number. Upon getting the users input, validate that the...
Prompt the user to input their lucky number. Upon getting the users input, validate that the input is a valid number using Int32.TryParse (more info can be found at https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number (Links to an external site.)). If the input is not a valid number, output a message to the user stating that you are sorry but you are unable to run the FizzBuzz process due to the input not being a valid number. If the input is valid, you are clear...
How do you validate in pseudocode a "REPEAT" "UNTIL" loop? For example if a user inputs...
How do you validate in pseudocode a "REPEAT" "UNTIL" loop? For example if a user inputs invalid data there should be an error message that reprompts for correct data. To show this can you write Pseudo Code for the following problem? Create an application that can determine the yearly bonus for all company employees. Not all employees are eligible for a yearly bonus. Only employees that have been with the company longer than 2 years and must have been given...
You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number.
You MUST use a while loop to print numbers 1-100. If the number is divisible by 3 print Soda instead of the number. If the number is divisible by 5 print Pop instead of the number. If the number is divisible by 3 AND 5 print *SP*.Print 10 numbers/words to a line. Display the numbers/words in right-aligned columns. Use printf statements to do this. For example:System.out.printf( "5d", number );    System.out.printf( "5s", "Soda" );Every time there are ten numbers on a...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT