Programming Assignment 5

Implement a class Car. A Car object should have three instance variables, one for fuel efficiency (measured in miles per gallon), one for fuel level (measured in gallons), and a variable that acts as an odometer. These instance variables should be private (see page 217 of the textbook). A Car object should accept the following messages (i.e., the Car class should contain the following methods). There should be an initialize method for initializing the instance variables. The fuel efficiency of a car should be specified as a parameter in the initialize method and this method should set the fuel level and the odometer to zero. (Later we will learn how to use a "constructor" to initialize an object.) There should be getFuelEfficiency, getFuelLevel, and getOdometer acessor methods. There should also be a method addFuel(double gallons) which adds a specified amount to the fuel level (and returns the new fuel level), and there should be a method drive(double miles) which simulates driving the car a specified distance. The drive method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel.

Write a client program TestDrive.java that tests your Car class. The test program should prompt a user for a fuel efficiency, construct and initialize a Car object, prompt the user for an amount of fuel, put the fuel in the Car object, prompt the user for a distance to travel, drive the appropriate distance, and then report back to the user the distance actually travelled, the current fuel level, and the current odometer reading. Your test program should let the user drive the car until the user enters a distance of zero. Then your test program should prompt the user for an amount of fuel to add to the car and then let the user drive the car some more. If the user enters zero for the amount of fuel to add, then your test program should prompt the user for a new fuel efficiency and construct and initialize a new Car object and let the user drive the new car. Your test program should terminate if the user enters zero for the fuel efficiency.

As part of grading your assignment, I will use this program, PreTestDrive.java, to test if your Car class implements the proper public interface (see pages 225-226 of the textbook) as specified in the first paragraph above. You should use this program to test your Car class before you write your own TestDrive.java program.

For this assignment you will be writing two files, one called Car.java and the other called TestDrive.java. Put your two files, along with a copy of SavitchIn.java, into a zip file with the name CS123Ass5Surname.zip and turn in the zip file. If you need help working with zip files, please ask me and I will show you what you need to know.

This assignment is due Monday, October 11.


Return to the main homework page.
Return to the main Java page.
Return to the CS 123 home page.


compliments and criticisms