Due: Friday, 16 September 2005 by 10 a.m.
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:
The program will then compute and print out the following four output values:
The program will have the following seven steps
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.
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.
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.
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.