Programming Assignment #8

Due: Fri, Dec 9

Objective:  Upon completing this assignment, you should be able to implement a simple class, as well as gain a better understanding of the building and use of classes and objects.

Task:

Write a class called Car, which has the features and member functions described below. This is based on Programming Challenge 13.3 in your textbook. You should write the Car class in the files car.h and car.cpp (like we saw for other class examples, like Fraction).

A car object will store information about the car's make, model, and year, as well as its current travel speed. Public member functions will allow access to the private information, the ability to set the car information, and the ability to speed up and slow down.

Details:

  1. Member data: The Car class should store the following information as private member data variables
     
  2. The Car class should have a constructor that takes in three arguments, in this order:
    1. the make
    2. the model
    3. the year (as an optional parameter)
    Note that the first two arguments (make, model) should always be provided when creating a car object, but the year may be optional. The constructor should initialize the data of a Car object to store the data provided in the arguments, and it should start the car's speed at 0 MPH.
    If a car object is created without the year provided, default the year to the current year (i.e. 2011).
    If the provided year is invalid (i.e. negative), default the year to the current year (2011)
    Examples of usage:
       Car hisCar("Ford", "Tempo", 1997);	// car set to be a 1997 Ford Tempo
       Car herCar("Honda", "Accord");	// car set to be a 2011 Honda Accord
       Car badCar("Toyota", "Camry", -50);  // car set to be a 2011 Toyota Camry
    
  3. The Car class should have a function called Set, which takes in three arguments -- the make, the model, and the year. This function should reset the car's internal information to the data passed in as long as the provided data is valid. IF the provided year is invalid (i.e. negative), this constitutes bad data, so do not make any changes to the car, and return false for failure. Otherwise, set the data accordingly and return true for success.
    Sample calls, based on initial objects above:
      hisCar.Set("Hyundai", "Sonata", 2006);   // hisCar is now a 2006 Hyundai Sonata
      herCar.Set("Horse Drawn", "Cart", -45);  // herCar is still a 2011 Honda Accord
    
  4. The Car should have accessor functions called: Each of these member functions should simply return a copy of the appropriate internal data item. Make sure to use appropriate return types for each. These functions will not change the object's internal data in any way.
     
  5. Create a member function called Display that prints out the data about the car to standard output, in this format:
    Your car is a 2005 Toyota Camry
    And it is currently going 17 MPH
    
    Note, the above is just an example. Your output should print the appropriate inforation for the given car. Using the previous examples, this call:
      herCar.Display();
    
    would print the following:
    Your car is a 2011 Honda Accord
    And it is currently going 0 MPH
    
  6. Create a function called Accelerate, which takes in a single character argument. The character will represent a level of pressure to apply to the accelerator. The choices are: The function should increase the car's speed based on the level of acceleration requested. If the level provided is invalid (i.e. some character other than the valid ones), do not change the car's speed at all, and return false for failure. Otherwise, modify the car's speed appropriately, and return true for success
     
  7. Create a function called Brake, which also takes in a single character parameter, representing the brake levels. These levels are the same ones as for acceleration, using the same characters (H, M, L, upper or lowercase). Note that the slowest possible speed for the car is 0. So if braking would cause the car to stop (i.e. 0 or below), set the speed to 0. (The car's speed can never be negative).
    As with the previous function, if the level is invalid (a character other than the valid ones), do not change the car's speed, and return false for failure. Otherwise, modify the car's speed, and return true for success.
     
  8. I have providing a sample driver program (called driver.cpp) that uses objects of type Car and illustrates some of the usage of the member functions. You can get the driver.cpp file at this link.

    I have also provided the output from the sample execution of my driver.cpp program at this link.  Your class declaration and definition files must work with my main program, as-is (do not change my program to make your code work!).

    Important Notes:


General Requirements


Submitting:

Submit the following files through the web submission page in the usual way:
 car.h
 car.cpp

Make sure your filenames are these exact names, and do not submit the driver.cpp file.