Create a program that reads data from a file regarding miles driven and gallons
ID: 3665222 • Letter: C
Question
Create a program that reads data from a file regarding miles driven and gallons of gas used and calculates the miles per gallon
The program:
Prompts the user for a file name.
Opens that file for processing
File is in the format: customer.dat is a sample file
Drivers name - a String
distance driven - an int
gallons of gas used - a double
Calculates the miles per gallon using the formula: distance driven / gallons used.
Displays the information in a nicely formated output. (must use printf)
Explanation / Answer
double miles; // Miles driven double gallons; // Gallons of fuel double mpg; // Miles-per-gallon // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); /** * Method should calculate miles per gallon * * @param miles * @param gallons * @return */ static double calculateMilesPerGallon(double miles, double gallons) { return miles / gallons; } // Describe to the user what the program will do. System.out.println("This program will calculate miles per gallon."); // Get the miles driven. System.out.print("Enter the miles driven: "); miles = keyboard.nextDouble(); // Get gallons of fuel System.out.print("Enter the gallons of fuel used: "); gallons = keyboard.nextDouble(); // call calculateMilesPerGallon to run calculation mpg = calculateMilesPerGallon(miles, gallons); // Display the miles-per-gallon. System.out.print("The miles-per-gallon is " + mpg);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.