please use java for this. write a program that does the following: • (1 Point) P
ID: 3816126 • Letter: P
Question
please use java for this.
write a program that does the following:
• (1 Point) Provide the following “menu” (choices) to the user:
o Enter trip data
o Show Eco Score
o Exit program
• Ask the user to choose from the menu
o (2 Points) Continue to show the menu (even with invalid choice – give error message but don’t exit) until the user chooses “Exit program.”
• For “Enter trip data”:
o (1 Point) Ask the user for:
- # miles traveled in the past 5 minutes
- Ask the user for gallons of gas consumed in the past 5 minutes
o (2 Points) Calculate total distance traveled, total gas consumed (gallons), MPG over the past 5 minutes, and cumulative MPG
o Ask the user if the user would like to add more data in 5 minute increments
- (2 Points) Allow the user to keep entering data in 5 minute increments until the user has no more data to enter
o(1 Point) Return to the menu when the user is done entering trip data
• For “Show Eco Score”
o (2 Points) Show total distance traveled, total gas consumed, and cumulative MPG
o (3 Points) Show an appropriate message (you decide the message) for when cumulative MPG is: Over 100, between 50 and 100, between 30 and 50, between 10 and 30, and below 10.
o (1 Point) Return to the menu after displaying message
Explanation / Answer
Hello, I have prepared this program in java,
I have kept the class name rider, I have explained the program as comments inside the program.
For better readability, paste the code below in a decent IDE then try to understand the code.
import java.util.Scanner;
public class rider {
public static void main(String[] args) {
boolean returnToMainMenu=false; // flag to decide whether to let user keep entering miles or redirect him to main menu
Scanner s = new Scanner(System.in);// we are using this scanner class to take user input
float miles=0; // this member will hold the tolta miles travelled by the user
float gallons=0;// this member will hold the total gallons of gas consumed by the user
String command="";// this member will hold the command entered by the user, initially it will be empty
while(!command.equals("3")){ // here we are using while loop, so that we can continue showin menu to the user based upon the command he/she enters
System.out.println("1. Enter trip data");// these are the menu options shown to the user
System.out.println("2. Show Eco Score");
System.out.println("3. Exit program");
command = s.nextLine();// here we are taking user input
if(!command.equals("1") && !command.equals("2") && !command.equals("3"))// validating the input entered by the user, if it is an invalid command then show him/her a message and continue the program flow
{
System.out.println("Invalid command");
}
switch(command)// we are using switch statement to process different logic based upon the user input
{
case "1" :// if user enters option 1, it means user wants to enter trip data so below code will be executed
while(!returnToMainMenu){
System.out.println("Enter miles traveled in the past 5 minutes :");
float tempmiles = Float.parseFloat(s.nextLine());// taking the user input for miles he/she travelled in last 5 minutes
System.out.println("Enter gallons of gas consumed in the past 5 minutes :");
float tempgallons = Float.parseFloat(s.nextLine());// taking the user input for gallons he/she consumed in last 5 minutes
miles+=tempmiles;// here we are adding the miles and gallons entered by user to the total miles and gallons
gallons+=tempgallons;
// Here we will show the specified output to the user based upon his/her inputs
System.out.println("Total distance travelled :"+miles);
System.out.println("Total gas consumed :"+gallons);
System.out.println("Your MPG for last 5 minutes was : "+tempmiles/tempgallons);
System.out.println("Your cumulative MPG till now is : "+miles/gallons);
System.out.println();// this will print just an empty line
// Showing the user a submenu, if they want to return to main menu then they will enter 2
System.out.println("1. Continue entering data for last five minutes");
System.out.println("2. Return to main menu");
//If user enters 2 then this while loop will discontinue and control will go to parent while loop which will show user the main menu
if(s.nextLine().equals("2"))
{
returnToMainMenu=true;
}
}
break;
case "2" :
System.out.println("Total distance travelled :"+miles);
System.out.println("Total gas consumed :"+gallons);
float cumMPG=miles/gallons;// calculating cumulative MPG
System.out.println("Your cumulative MPG till now is : "+cumMPG);// printing the cumulative MPG
// here we are printing different messages based upon cumulative MPG
if(cumMPG>=100)
{
System.out.println("Bravo! Efficiency at it's best.");
}else if(cumMPG<100 && cumMPG>=50)
{
System.out.println("Good efficency.");
}else if(cumMPG<50 && cumMPG>=30)
{
System.out.println("Moderate efficency.");
}else if(cumMPG<30 && cumMPG>=10)
{
System.out.println("Not good efficency.");
}else if(cumMPG<10)
{
System.out.println("Bad efficency, You must be stuck in traffic jam.");
}
break;
}
System.out.println();
}
}
}
If you have any questions regarding this solution then please do comment.
Good luck.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.