Java Project: Fuel Efficiency

The United Nations (UN) has set forth 17 goals for sustainable development. Goal 12 is designed to “Ensure sustainable consumption and production patterns.” Consumption of natural resources is occurring at a record pace, and both nations and organizations are enacting policies designed to use (and replenish) our resources in a responsible manner. Such policies can help to limit the effects of climate change. The move towards renewable energies, as well as advances in technology for older energy sources, are examples of such policies.

Fossil fuel consumption is one exacerbating factor in climate change. In this project, we will consider the differing costs and efficiencies of fossil fuel-based cars and hybrid-based cars.

Fuel Efficiency

Automobile exhaust is a major contributor to air pollution. According to the Union of Concerned Scientists, personal vehicles contribute nearly one-fifth of U.S. emissions [1]. In order to reduce the amount of emissions, more fuel efficient cars are being developed. Fuel efficient cars can also save drivers money at the gas pump, or even eliminate gas costs altogether.

There are many different types of technology being developed to improve fuel efficiency, including hybrid gas/electric, hydrogen fuel cells, biodiesel, and solar. You might ask the question, are these types of cars really “cleaner” than traditional gasoline-only cars? This video discusses this question:

Where Can I Learn More?

Here are some links to additional information on this topic:

For this problem, you will compare the fuel efficiency for gas-only cars vs. hybrid cars. A car’s fuel efficiency is measured in how many miles can be driven per gallon of gas (mpg).


Java Project: Automobile Fuel Efficiency

This project will provide you with an introduction to the fundamentals of problem-solving in Java. The focus of this project is on how to solve simple problems using Java primitive data types, variables, and arithmetic operations. At the end of this project, you will…

  • Develop code to read data from the standard input and produce data to the standard output.
  • Translate a given mathematical expression into equivalent syntactically correct programming statements.
  • Write code that conforms to a set of commonly accepted style guidelines.

As you write your code, be sure to properly document your code. Consult the Java Coding Guidelines document (provided by the instructor) for style expectations. Well-written code is easier to debug, easier to maintain over time, and easier to extend as new requirements arise.

Exercise #1: Create a Java class called FuelEfficiency, and save the file as FuelEfficiency.java in your Java development environment. This program will ultimately compute the amount of power produced by a wind turbine, but we will be building the program in stages. In this exercise, we will take care of the user input. Your program should take two double inputs:

  • The miles per gallon (MPG) for a gas car
  • The miles per gallon (MPG) for a hybrid car

Your program should create two variables to store these values, followed by appropriate user prompts and input statements (Hint: use the Scanner class). Once the input is complete, your program should end. Here are few sample tests you can run to check if your program is working correctly; your prompts should appear exactly as shown here, and you can type in the listed input values when prompted:

Test Case #1

Enter the MPG for a gas car: 19.0
Enter the MPG for a hybrid car: 28.0

Test Case #2

Enter the MPG for a gas car: 22.50
Enter the MPG for a hybrid car: 33.50

Exercise #2: Now that you have the input statements complete, you will add code to echo-print the input values to the user along with descriptive messages and units. This will simply involve adding two System.out.printf statements to your code. All numeric amounts should be precise to two decimal places: remember this involves using the %f edit descriptor with the System.out.printf method, as shown below:

/* 
   Show two digits on the right of the decimal for the value 
   found in variable "num"... 
*/
System.out.printf("%.2f", num);

Here are few sample tests you can run to check if your program is working correctly:

Test Case #3

Enter the MPG for a gas car: 19.0
Enter the MPG for a hybrid car: 28.0

The gas car achieves 19.00 miles per gallon.
The hybrid car achieves 28.00 miles per gallon.

Test Case #4

Enter the MPG for a gas car: 22.50
Enter the MPG for a hybrid car: 33.50

The gas car achieves 22.50 miles per gallon. 
The hybrid car achieves 33.50 miles per gallon.

Exercise #3: Now you are ready to perform some calculations. Add two double variable declarations to your code to store the number of gallons of gas used over the span of 100,000 miles. Compute the amount of gas used (in gallons) in 100,000 miles for each type of car. Store the results in your two variables. and add one or more System.out.printf statements to your code to display the contents of those variables. Here are few sample tests you can run to check if your program is working correctly:

Test Case #5

Enter the MPG for a gas car: 19.0
Enter the MPG for a hybrid car: 28.0

The gas car achieves 19.00 miles per gallon.
The hybrid car achieves 28.00 miles per gallon.

The gas car uses 5263.16 gallons per 100000 miles.
The hybrid car uses 3571.43 gallons per 100000 miles.

Test Case #6

Enter the MPG for a gas car: 22.50
Enter the MPG for a hybrid car: 33.50

The gas car achieves 22.50 miles per gallon. 
The hybrid car achieves 33.50 miles per gallon.

The gas car uses 4444.44 gallons per 100000 miles.
The hybrid car uses 2895.07 gallons per 100000 miles.

Exercise #4: Let’s add two final calculations. Add two more double variable declarations to your code to store the cost of gas used over the span of 100,000 miles. Compute the cost of gas used (in gallons) over 100,000 miles for each type of car (assume gas is $2.95/gallon). Store the results in your two variables. and add one or more System.out.printf statements to your code to display the contents of those variables. Here are few sample tests you can run to check if your program is working correctly:

Test Case #7

Enter the MPG for a gas car: 19.0
Enter the MPG for a hybrid car: 28.0

The gas car achieves 19.00 miles per gallon.
The hybrid car achieves 28.00 miles per gallon.

The gas car uses 5263.16 gallons per 100000 miles.
The hybrid car uses 3571.43 gallons per 100000 miles.

The gas car costs $15526.32 per 100000 miles.
The hybrid car costs $10535.72 per 100000 miles.

Test Case #8

Enter the MPG for a gas car: 22.50
Enter the MPG for a hybrid car: 33.50

The gas car achieves 22.50 miles per gallon. 
The hybrid car achieves 33.50 miles per gallon.

The gas car uses 4444.44 gallons per 100000 miles.
The hybrid car uses 2895.07 gallons per 100000 miles.

The gas car costs $13111.10 per 100000 miles.
The hybrid car costs $8805.97 per 100000 miles.

Your program is now complete.

Deliverables

See the instructor for submission instructions and due date(s).

Project created by Tricia Clark (tkc3@psu.edu); Modified by Jeffrey A. Stone (stonej@psu.edu). 

Powered by WordPress. Designed by WooThemes

Skip to toolbar