ESc 014

Assignment 7 -- The Automobile Simulator
Due Tuesday, May 28, 2002



Classes provide a way to group together into one place the variables and functions that pertain to one "entity" in your program. We call such an "entity" an object. For example, you could think about your last program as pertaining to duck objects. Each duck object had variables describing it; namely, the first name of its sponsor or its finishing place. By putting all duck-related information into a struct, we were able to organize it in a single array. In the same way, we can organize object-related information into a class and then use it in a very streamlined way.

Your Mission

Your assignment is to write a class to simulate a working car. Your car will have

One of the neat things that computer programs can be used for is simulations of real-world things like you're doing here. Instead of creating an expensive and/or dangerous prototype of something new, you can first create it on the computer to model the behavior of the real thing. You can then decide what changes should be made before actually building it.

The Details

As shown in class, you need at least three files to use classes effectively: a header file, a file for the class itself, and a third file to actually use the class in some way. You'll be writing the first two, and I'm giving you the third. Download "carTest.cpp" from BlackBoard in the Course Materials section. It contains the main function that you'll be using this time around. Besides the main function, "carTest.cpp" contains three functions that are really just a series of tests to make sure your class is working correctly. After reading this assignment thoroughly, you should read the source code in "carTest.cpp" to understand what it is doing. You are (strongly) encouraged to add your own testing functions to "carTest.cpp" once your program is working to make sure you know how to use the class yourself (and to have fun with your working car!)

File #1: the header file

Your header file, which you should call car.h, is where the car class is defined. The basic structure of your car class will look like this (where you replace the italicized text):
#ifndef some variable you make up goes here
#define same variable as above
class car  
		// function prototypes go here
		// variables related to car go here
};  // don't forget the semicolon!
The header file contains all the function prototypes for your class (in the public section) and all the variables that pertain to your car (in the private section). Be sure to understand what the #ifndef, #define, and #endif commands are for (review p. 486 in your text).

The private variables you need to define are:

Now for the functions. Their prototypes will go in the public section while their definitions will go in the second file: the class itself. We'll talk about them next.

File #2: the class file

Your class file, which you should call car.cpp, is where the functions for the car class are implemented. Here is a list of the ones you need to write and what their jobs are. Remember to put their prototypes in the header file.
   // default constructor.  Remember, the constructor's job
   // is simply to initialize the private variables.  The
   // six variables in the car class should be 
   // initialized as follows:
   // - the owner will be the generic name "default"
   // - put the gear in park
   // - distance traveled will be zero
   // - the car will get 20 miles per gallon
   // - the gas tank will hold 10 gallons max
   // - the car will begin with a full tank of gas

car::car(int MPG, int capacity, string newOwner)  
   // non-default constructor.  The given parameters
   // will be used to initialize the miles-per-gallon
   // variable, the capacity of the gas tank, and
   // the owner's name, respectively.  The other
   // private variables should be initialized in the
   // same way that the default constructor dictates

void car::go(int miles)
   // this moves the car the number of miles specified 
   // in the parameter.  It moves the car forward or
   // backward depending upon what gear the car is in.
   // A car will not move if its gear is in park.
   // Remember, the total forward distance traveled will
   // decrease if the car is moving backwards.
   // Here is the algorithm for what this function does:
   // if the car is in park
   //    car can't move; display an error message
   // else
   // {
   //   compute amount of gas used
   //   if car has enough gas to proceed
   //   {
   //      Move car forward or backward the appropriate amount and
   //      decrease the amount of gas by the appropriate amount.
   //      Note that if a car goes backward beyond its starting 
   //      point, the total distance traveled will become negative.
   //   }
   //   else car does not have enough gas to proceed
   //   {
   //      compute distance that car can move with the gas remaining
   //      move car forward or backward by that amount and
   //      set gas amount to zero  
   //   }
   // }
   // In all cases, the user should be informed as to how far
   // the car moved, in what direction, and the amount of gas used

void car::fillTank()
   // fills gas tank up to capacity

void car::putinGas(int fuelAmount)
   // Fills gas tank with given amt of gas.
   // If the tank goes over capacity, the
   // current amount of gas will be equal
   // to the tank capacity (don't overflow)

double car::getGas() const
   // returns the current amt of gas car has

int car::getCapacity() const
   // returns fuel tank capacity

int car::getDistance() const
   // returns forward distance car has traveled

int car::getMPG() const
   // returns miles per gallon rating on car

string car::getOwner() const
   // returns owner of car

string car::getGear() const
   // returns current gear as a string
   // i.e. "forward", "reverse", or "park"

void car::setGear(direction newGear)
   // sets gear to park, forward, or reverse

void car::showDashboard() const
   // print all stats for the car to cout.
   // Displays the owner, gas tank capacity,
   // amount of gas remaining, forward distance
   // traveled, miles per gallon, and current gear
   // the car is in

File #3: the tester file

Once you've got the header and class files working correctly, you should be able to use them with the "carTest.cpp" file I provided. Be sure to use capital and lower-cases letters in your function names just like I have above or else the tester file won't work. Now you can construct cars, put them in gear, move them backwards and forwards, watch them run out of gas, and refuel them!

Remember the steps you take in Visual C++ when compiling a project with multiple source code files:

  1. Compile the file that uses the class (probably the one that contains the main function). For this assignment, that would be "carTest.cpp"
  2. In the Project menu, select Add to Project and then Files...
  3. Select the other source code files from the menu that pops up (car.cpp in this case). You do NOT need to add the header file to the project this way. That will be included automatically due to the #include lines you put into your code.
  4. Your "carTest.cpp" file should now be runnable.

Turning this in

As always, paste in your test results at the bottom of your source code and turn in both a hard copy and an electronic copy. Because you will have three files in your project, please put them into a folder named with your name and zip them up into a single file using a utility like winZip and then upload that single file to BlackBoard. In the Olin 110 lab, you can do this by right-clicking on the folder that you want to zip up and then choosing the "Add to Zip" command in the menu that pops up. See me if you're having problems doing this.

Classes are powerful constructs in computer programming, but they can take some time to get used to. Start this project early. If you wait until the last minute, you may not get it all done.


50 points will be divided as follows:

Administrative statement

This is an individual project. I encourage you to talk to others about the general nature of the project and ideas about how to pursue it. However, the technical work, the writing, and the inspiration behind these must be substantially your own. If any person besides you contributes in any way to the project, you must credit their work in your report. Similarly, if you include information that you have gleaned from other published sources, you must cite them as references. Looking at, and/or copying, other people's programs or written work is inappropriate, and will be considered cheating.

Return to Assignment Index