Programming Assignment 1 Solution



This assignment makes use of the files contained in this zip file. This assignment is due Wednesday, September 2.

In this assignment you will write a class called that can “simulate” some of the behavior of a real car. This assignment is based on material from Sections 2.1 & 2.2 of our textbook (and what you learned in CS 12300). Download this zip file and un-zip it. In the zip file there is a copy of this web page, which you can read in your browser, and some files that you need for this assignment.

Implement a class Car. A Car object should have three (private) instance variables, one for fuel efficiency (representing miles per gallon), one for fuel level (representing gallons), and a variable that acts as an odometer (representing miles). The fuel efficiency of a car should be specified as a parameter in a Car constructor and the constructor should set the fuel level and the odometer to zero. There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods. There should also be a method addFuel(double
which adds a specified amount to the fuel level and returns the new fuel level, and there should be a method drive(double
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. (Notice that there are setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer should only be changeable by driving the car, as in a real car. The fuel level should only be changed by driving the car or by adding fuel.)

Your file should include Javadoc specifications for the class and each method, as described on pages 6 – 9 and Appendix H of our textbook. Be sure to run the javadoc compiler on your file to produce a Car.html file.

In the zip file there is a file This file tests the public interface of your Car class. After you have written your version of, you should be able to compile and run When you run it, it should report that there are no problems with your Car class.

Write a client program that uses your Car class. This program should prompt a user for a fuel efficiency, construct 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 traveled, the current fuel level, and the current odometer reading. Your program should let the user drive the car until the user enters a distance of zero. Then your 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 program should prompt the user for a new fuel efficiency and construct a new Car object and let the user drive the new car. Your program should terminate if the user enters zero for the fuel efficiency.

In the zip file there is a program TestDriveDemo.class that is a demonstration version of the client program you are to write. After you have written your version of, you should be able to run TestDriveDemo.class. Your version of should work exactly like the demo version!

As part of grading your assignment, I will use the program (from the zip file), to test if your Car class implements the proper public interface as specified in the paragraphs above. I will also generate a Javadoc file from your And then I will compile and run your version of to make sure that it works exactly like TestDriveDemo.class.

Turn in a zip file called (where Surname is your last name) containing your versions of and

error: Content is protected !!