Programming Assignment 7 Due April 26, 2015 at 11:59 PM Objective

advertisement
Programming Assignment 7
Due April 26, 2015 at 11:59 PM
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. 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:
-
-
Member data: The Car class should store the following information as private member data
variables
o The car's make (examples: Toyota, Ford, Honda). This will be a string.
o The car's model (examples: Camry, Tempo, Accord). This is also a string.
o The year of this car model, stored as an integer. The class will need to enforce this
invariant rule -- year cannot be a negative number.
o The current speed that the car is traveling (an integer). The assumed units are miles-perhour, so if the speed is 35, this means 35 MPH. The speed must be a non-negative number
The Car class should have a constructor that takes in three arguments, in this order:
o the make
o the model
o the year (as an optional parameter)
o 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.
o If a car object is created without the year provided, default the year to the current year
(i.e. 2015).
o If the provided year is invalid (i.e. negative), default the year to the current year (2015).
Examples of usage:
Car hisCar("Ford", "Tempo", 1997);
Car herCar("Honda", "Accord");
// car set to be a 1997 Ford Tempo
// car set to be a 2015 Honda Accord
Car badCar("Toyota", "Camry", -50);
// car set to be a 2015 Toyota Camry
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 2015 Honda Accord
The Car should have accessor functions called:




GetMake -- returns the car's make
GetModel -- returns the car's model
GetYear -- returns the car's year
GetSpeed -- returns the current speed
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.
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 2015 Honda Accord
And it is currently going 0 MPH
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:



Heavy acceleration ('H' or 'h')
Medium acceleration ('M' or 'm')
Light acceleration ('L' or 'l')
The function should increase the car's speed based on the level of acceleration requested.



Heavy acceleration increases speed by 10 MPH
Medium acceleration increases speed by 5 MPH
Light acceleration increases speed by 1 MPH
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.
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).



Heavy braking should decrease the speed by 10 MPH
Medium braking should decrease the speed by 5 MPH
Light braking should decrease the speed by 1 MPH
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.
I have provided a sample driver program (called driver.cpp) that uses objects of type Car and
illustrates some of the usage of the member functions.
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:


This driver program is just a SAMPLE. It is not the only program that could be written that
uses the Car class.
This driver program does NOT test every possibility, and you should not assume that it
does. The specification of each function is above (the sample driver program is NOT the
assignment specification -- it's just an example). You need to write your own calls to test
your class more thoroughly.
Example: Note that this driver program calls the Set function, but it does not actually check the
return value (true or false). To test this, you would need to write your own calls and check the return
value to make sure your function works as described. Example:
bool answer = Set("Ford", "Tempo", 1996);
// now check the answer variable
In writing your own test cases and function calls, you may feel free to add to the driver.cpp
program, or you can write a separate main() program of your own. You will not be submitting any main
programs -- you will only submit your class files.
General Requirements:


No global variables, other than constants!
All member data of your class must be private


All member functions described in this specification should be public.
For storing strings, you may use string objects or C-strings -- your choice. (I'd recommend
using string objects, as they will be easier to work with).
 You may use any of these libraries:
 iostream
 iomanip
 cctype
 cstring
 string
Your source code should be readable and well-documented.
Your car.h file should contain the class declaration only. The car.cpp file should contain the member
function definitions.
Submitting:
Submit the following files through Blackboard in the usual way by April 26, 11:59 PM:
car.h
car.cpp
Make sure your filenames are these exact names, and do not submit the driver.cpp file.
Download