# Homework J1

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

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

## Purpose

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

## Design

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.

• Header: The comments at the beginning of the file, and need to include your name, e-mail ID, 101 section (or 101-E, if appropriate), and a line or two about what this program does.

• Comments: A line or two before each major "block" (section of code, class, method, variable, etc.) describing what it does.

• Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.

• Legend: The program should print out a line or two that states what the purpose of the program is (i.e. "This is a BMI calculator", or "This program computes the mileage for a car trip"). This should be the first line(s) printed.

• Echoing input: All user input should be echoed back to the screen. This can be a line saying "you entered 5" after the user enters each input, but can also be a line saying, "The product of the two numbers you entered, 5 and 7, is 35".

• Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH. Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.

• Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not wrap to the next line. You can have a println statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator has a vertical grey line around the 70th column, so you can use that to gauge how long your lines are.

• Test code: ALL programs must include proof that you tested the program.  For this program, the test is just required as comments - enter what values you used to test your program, and what the output is.  Just inputting random values will not get you any points here - you need to think about what sort of values will sufficiently test your program, as well as what values you can hand-check to make sure the program works as desired.

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.

• Program header (includes name, e-mail ID, and purpose)
• Good commenting and use of whitespace
• Print out an appropriate legend
• Proper use of Scanner for the input stream
• Properly prompts user for input
• Input is echoed to the screen
• Properly computes the necessary values
• Prints out the necessary values
• Variable declaration (good names, proper initialization, etc.)
• Testing cases shown in comments

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).

## Submission

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