Homework J1

Due: Friday, 16 September 2005 by 10 a.m.

Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums


For this homework, you are to write a program that will compute the gas mileage for a tank of gas in an automobile.  The user will have to input four values:

  1. The initial odometer reading (how many miles the car had before the tank of gas)
  2. The final odometer reading (how many miles the car had after the tank of gas)
  3. The amount of gas used (in gallons)
  4. The cost per gallon of gas (in dollars)

The program will then compute and print out the following four output values:

  1. The total miles driven
  2. The mileage for that tank of gas in miles/gallon
  3. The mileage for that tank of gas in kilometers/liter
  4. The amount spent per mile driven



The program will have the following seven steps

  1. Define the constants you are using.  In particular, there are 1.609344 kilometers per mile and 3.7854118 liters per gallon.
  2. Print out an appropriate legend (see below)
  3. Set up the input stream (i.e. create a new Scanner object)
  4. Ask the user for the four inputs described above, and read in those input values
  5. Echo back the input to the user (see below)
  6. Compute the four output values described above
  7. Print out the four output values described above (remember that if you want to print out multiple things with a System.out.println() statement, you need to separate them with a + (a plus sign)).

You should be able to determine the formula for computing the four output values - see a TA or professor for help if you are unsure about them.  When asking for input, please only ask for those 4 values - the program is going to be run by another program, so if you ask for more input values, your program will not execute correctly for the graders.

While we have provided most of the design for this program, you need to provide some design yourself - in particular, you need to figure out the formula for computing the various output values, what variables will be needed to store them, and how to implement those computations.


How and where to start

We have provided a GasMileage.java file for you to start with.  Changing the class name will cause a loss of lots of points.

Look at the code for BMICalculator.java (page 75 of the textbook, and available online here) for an idea how to start, as that program is somewhat similar to the one that must be written for this homework.  The 8 parts of the main() method for BMICalculator.java (each part is separated by a blank line in the textbook) will be replaced by the 7 parts (mentioned above) for GasMileage.java.

The best way to proceed is to go through the seven steps described above, testing the program after each step or two.  For example, after you write the code to read in the input from the user, you should print those values out to the screen, so that you can test your input routines.  You are welcome to try to write the whole program and debug it all at once - however, this will take you much more time than testing it incrementally.


Good programming practices

The following are considered good programming practices.  All homeworks must include these whenever possible.  Not including them will receive points off from the grade.  The BMICalculator.java program (on page 75 of the textbook) is an example of a program that follows these practices.

When we test your program, we will only use positive values (i.e. greater than zero) for the input values, and the final mileage will always be greater than the initial mileage.  You only need to test with these types of values as well.


Sample execution

Note that the text in red is what was input by the user.  Your program needs to print out similar information, but the format does not have to be the same.

This program will compute the gas mileage

Enter the initial mileage: 105820
Enter the final mileage: 106103
Enter the gas used (gallons): 14.347
Enter the price per gallon: 2.999
You spent $2.999 per gal on 14.347 gals of gas from mileage 105820 to 106103
You traveled 283.0 miles
The mileage is 19.725378127831604 miles/gallon
The mileage is 8.38612035228427 kilometers/liter
You spent $0.15203764310954065 per mile driven



The following lists the criteria that the graders will be look at when the homework is graded.  Note that these criteria will not be provided on the other homeworks, only on this first one.  Also, the points for each of the following criteria have not yet been determined.

If your program does not compile, you will receive 25 points off, in addition to any other penalties.  If you do not understand a compilation error, please see a TA or professor (thus, you probably don't want to wait until the night before to start this homework).



When you are finished, submit the GasMileage.java file.