Using a Scanner to read input. Working with variables and
expressions.
You are going to write a program to perform a fuel cost and
CO2 pollution comparison of a gasoline car versus an
electric car. Your program should be organized into
six sections:
- Input values for gas car
- Gas car calculations
- Print analysis for gas car
- Input values for electric car
- Electric car calculations
- Print analysis for electric car and savings analysis
An example run of your program, using realistic values,
should look like this:
What is the current price per gallon of gasoline ? 3.00
How many miles per gallon does your car get? 25
You use 480.0 gallons of gas per year.
This costs $1440.0 and produces 9072.0 lbs of CO2 pollution
Now consider an electric car.
How many kilowatt hours (kwh) does it use per 100 miles? 30
What is the cost/kwh of your electricity in dollars 0.149
That electric car would use 3600.0 kwh's of electricity per year.
This costs $536.4 and produces 2117.3399999999997 lbs of CO2 pollution.
That would save $903.6 per year, and reduce carbon emissions by 6954.66 lbs.
(Later in this class we will learn how to round off and format printed values so you don't print all those excessive digits of precision.)
First you will ask the user for some information about
their gasoline car. Get the 3 values shown above. For each
value, print a prompt and then read the value using a
Scanner. Use scnr.nextDouble(); to get input values, and
store them in variables of type double. Use meaningful
variable names such as: milesDriven,
gasPrice, and mpg.
In the second section calculate the number of gallons of gasoline used per year, the cost for that gasoline, and the CO2 pollution produced by using it.
Include this constant declaration in your program, and use
it to calculate the CO2 pollution:
final double LBS_CO2_PER_GALLON_E10 = 18.9; // pounds of CO2/gallon of E10 gasoline
(E10 is gasoline with 10% ethanol currently used in the US.)
Store your calculated results in more variables.
Print these values just like in the report above.
Now ask the user for the electric car values. Use
meaningful variable names.
Next calculate the number of kwh's used per year, the cost
of that much electricity, and the CO2 pollution produced by
using it.
Include this constant declaration in your program, and use
it to calculate the CO2 pollution:
final double LBS_CO2_PER_KWH =
0.58815; // pounds of CO2/KWH
Print the rest of the report including the cost savings and
CO2 reduction.
The input values in the above example are based on national
and local averages and prices. Use these values and verify
that your program calculates the exact same results. Now run
your program with input based on your own vehicle, usage,
and prices, and for a different electric car.
Here are electricity usage values for some current electric cars:
Electric car |
kwh/100 miles |
Tesla Model 3 | 24 |
Hyundai Ioniq | 25 |
Chevy Bolt |
29 |
Nissan Leaf | 31 |
Mustang Mach E |
34 |
Ask the user how many years they expect to own the car, and
then calculate and print the expected fuel cost savings and
CO2 reduction over the lifetime of the car.
Turn in your program and its output for both test cases.
Please format your report neatly, and make sure your code is
properly indented.