In this task the student will create a new program that calculates gas mileage i
ID: 3836122 • Letter: I
Question
In this task the student will create a new program that calculates gas mileage in miles per gallon. The student will use string expressions, assignment statements, input and output statements to communicate with the user. 1. Create a new file in your IDE or text editor. 2. Create the shell for your first program by entering: public class Mileage {public static void main(String[] args) {//add your declaration and code here}} 3. Save the file as Mileage.java. 4. Translate the algorithm below into Java. Don't forget to declare variables before they are used. Each variable must be one word only (no spaces). Print a line indicating this program will calculate mileage Print prompt to user asking for miles driven Read in miles driven Print prompt to user asking for gallons used Read in gallons used Calculate miles per gallon by dividing miles driven by gallons used Print miles per gallon along with appropriate labels 5. Compile the program and debug, repeating until it compiles successfully. 6. Run the program and test it using the following sets of data and record the results: 7. The last set of data caused the computer to divide 100 by 0, which resulted in what is called a runtime error. Notice that runtime can occur on programs which compile and run on many other sets of data. This emphasizes the need to thoroughly test you program with all possible kinds of data.Explanation / Answer
//Find java code with output here
import java.util.Scanner;
public class Mileage {
public static void main(String[] args) throws ArithmeticException {
System.out.println("This program will calculate mileage.");
Scanner sc = new Scanner(System.in);
//Here taking user input for miles driven
double drivenMiles;
System.out.print("Please enter miles driven: ");
drivenMiles = sc.nextDouble();
//Here taking user input for gallons used
double gallonsUsed;
System.out.print("Please enter gallons used: ");
gallonsUsed = sc.nextDouble();
//Here calculating miles driven per gallons(miles/gallons)
double milesPerGallon=0;
if(gallonsUsed>0){
milesPerGallon = drivenMiles/gallonsUsed;
}else{
throw new ArithmeticException("Gallons used can not be zero, Please provide valid input.");
}
//Here displaying inputs and outputs
System.out.println("---------Input/Output is displaying here.....");
System.out.println("Miles driven: "+drivenMiles);
System.out.println("Gallons used: "+gallonsUsed);
System.out.println("Miles/Gallons: "+milesPerGallon);
//System.out.println("Miles driven: "+drivenMiles+", "+"Gallons used: "+gallonsUsed+", "+"Miles/Gallons: "+milesPerGallon);
}
}
//Outputs are here.....
//First output
This program will calculate mileage.
Please enter miles driven: 2000
Please enter gallons used: 100
---------Input/Output is displaying here.....
Miles driven: 2000.0
Gallons used: 100.0
Miles/Gallons: 20.0
//second output
This program will calculate mileage.
Please enter miles driven: 500
Please enter gallons used: 25.5
---------Input/Output is displaying here.....
Miles driven: 500.0
Gallons used: 25.5
Miles/Gallons: 19.607843137254903
//Third output
This program will calculate mileage.
Please enter miles driven: 241.5
Please enter gallons used: 10
---------Input/Output is displaying here.....
Miles driven: 241.5
Gallons used: 10.0
Miles/Gallons: 24.15
//Fourth output
This program will calculate mileage.
Please enter miles driven: 100
Please enter gallons used: 0
Exception in thread "main" java.lang.ArithmeticException: Gallons used can not be zero, Please provide valid input.
at Mileage.main(Mileage.java:27)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.