Write a program that helps a person decide whether to buy a hybrid car. Your pro
ID: 3847984 • Letter: W
Question
Write a program that helps a person decide whether to buy a hybrid car. Your program's inputs should be: • The make and model of the car • The cost of a new car • The estimated miles driven per year • The estimated gas price • The efficiency in miles per gallon Compute the total cost of owning the car for five years. (For simplicity, we will not take the cost of financing into account.). Compute the estimated resale value after 5 years (40% of initial cost). Obtain realistic prices for a new hybrid and a comparable gasoline fueled car from the Web. Run your program twice, using gas prices of $1.98 per gallon and 15,000 miles per year. Include pseudocode, the inputs and outputs of both of your examples, and the .java program file with your assignment.
Explanation / Answer
import java.util.Scanner;
public class HybridCar {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// prompt to enter the input
System.out.print("Please enter the make of the car:");
String make = scanner.next();
System.out.print("Please enter the model of the car:");
String model = scanner.next();
System.out.print("Please enter the cost of the car:");
double carCost = scanner.nextDouble();
System.out
.print("Please enter the miles will be driven in a year:");
double miles = scanner.nextDouble();
System.out.print("Please enter the gas price:");
double gas = scanner.nextDouble();
System.out.print("Please enter miles per gallon:");
double mpg = scanner.nextDouble();
// estimated resale value after 5 years (40% of initial cost)
double resale = carCost * 0.4;
// calculate
double efficiency = gas / mpg;
double milesDriven = miles * efficiency * 5;
double usedCarCost = carCost - resale;
double total = usedCarCost + milesDriven;
// print the result
System.out.printf(
"The total cost of owning the car for five years: %,.2f",
total);
} catch (Exception e) {
// TODO: handle exception
} finally {
scanner.close();
}
}
}
OUTPUT:
Please enter the make of the car:Honda
Please enter the model of the car:H111
Please enter the cost of the car:500000
Please enter the miles will be driven in a year:15000
Please enter the gas price:1.98
Please enter miles per gallon:12
The total cost of owning the car for five years: 312,375.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.