Programming Assignment 1
CS 27500
Data Structures
Fall, 2021

This assignment is due Monday, September 6.

This assignment makes use of the files contained in this zip file.

In this assignment you will write a class called Car.java that can "simulate" some of the behavior of a real car.

This assignment is based on material from Programming in the Large II: Objects and Classes (from this book) and Defining Object Behavior (from this book).

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 also be a second Car constructor that has two parameters that initialize the fuel efficiency and the fuel level (and the constructor should set the odometer to zero). There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods. There should be a method addFuel(double gallons) which adds a specified amount to the fuel level and then returns a reference to the current Car object. 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. Notice that there are no setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer's value should only be changed by driving the car, as in a real car. The fuel level's value should only be changed by driving the car or by adding fuel. Here is a summary of the constructors and methods (the public interface) of the Car class.

Here is a brief explanation of the return value for the addFuel() method. This method is supposed to return a reference to the current object. This is kind of an advanced (but important) programming trick. The reason for returning a reference to the current Car object is so that you can "chain" method calls on the Car object. Here is an example of chaining method calls.

     double gallons = car.addFuel(10).getFuellevel();

Notice that this line of code has two method calls. First, the addFuel() method is called on the object referred to by car. The addFuel() method returns a reference to the object referred to by car so we can call another method on that object, the getFuelLevel() method. The return value of getFuelLevel() is what is stored in the gallons variable.

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

PreTestDrive.java uses your Car's toString() method to find out what is stored in a Car object. Look carefully at the code in PreTestDrive.java to find out what kind of String your toString() method should return.

Write a client program TestDrive.java 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 Car.java (and it has passed the PreTestDrive tests), you should be able to run TestDriveDemo.class. You run the TestDriveDemo.class program from a "command-line". Here is how you do that. First, make sure the Car.java, Car.class, and TestDriveDemo.class files are all in the same directory folder. Use your keyboard and mouse to "Shift-Right-Click" on an empty space in that directory folder and select the pop-up menu item "Open command window here". You should now see a "command prompt" window. Click on this window and type the following command. (Notice that hw1> is the command-line "prompt". You type what comes after the prompt.)

      hw1>java TestDriveDemo

That command will run the demo program and let you experiment with how your program should work. Your version of TestDrive.java should work exactly like this demo version!

NOTE: TestDriveDemo.class was compiled using Java 11. So you need to compile your Car.java file using at least version 11 of Java. For example, you can use Amazon Corretto Java 11.

There is another test file, TestDriveScript.txt, of sample inputs for your TestDrive program. You run your compiled TestDrive.class program using the sample input with a command-line like the following (first make sure the Car.class, TestDrive.class, and TestDriveScript.txt files are all in the same directory folder and open a command-line prompt at that folder as above).

      hw1>java TestDrive < TestDriveScript.txt

Your program should produce output exactly like the TestDriveDemo.class program.

      hw1>java TestDriveDemo < TestDriveScript.txt

As part of grading your assignment, I will use the program PreTestDrive.java to test if your Car class implements the proper public interface as specified in the paragraphs above. Then I will compile and run your version of TestDrive.java using TestDriveScript.txt to make sure that your TestDrive works exactly like TestDriveDemo.

Turn in a zip file called CS275Hw1Surname.zip (where Surname is your last name) containing your versions of Car.java and TestDrive.java.

This assignment is due Monday, September 6.