Project 5
Templates
Assigned |
April 28 2003 |
Program Due |
Sunday May 11, 2003, 11:59pm |
Updates |
April 29, 12:45pm
In the Command File section, there was a naming inconsistency between
UNLOAD-BOX and DELIVER-BOX (same for SACK). UNLOAD-BOX and UNLOAD-SACK
have been changed to DELIVER-BOX and DELIVER-SACK. These are the commands
which will be found in the command file.
In the Truck class template, special syntax is needed to declare operator<<
a friend. Use the syntax
friend ostream& operator<< (....);
Note the <T>.
Note also that this applies ONLY to the friend declaration
in the .H file, NOT to the function header in the .C file.
|
Objectives
- To gain experience designing and implementing a class template
- To gain experience writing function templates
Project Description
In this project you will implement a class to model a delivery truck.
Since trucks can carry any type of cargo, it is appropriate to use
a C++ class template for the truck. You will also implement classes
to model boxes and sacks carried in the truck.
Your project will simulate exactly two trucks -- one containing boxes, the other
containing sacks. Your truck will load boxes/sacks, drive from one destination
to another, deliver boxes/sacks and print an inventory of the truck's
contents. Commands read from a command file
direct your trucks.
The command line will have three arguments
- The maximum number of boxes a truck can carry regardless of
the box's size and weight.
- The maximum number of sacks a truck can carry, regardless of the
sacks contents or weight.
- The name of the command file
Class Definitions
For this project you will implement the Truck class template, the Sack class and
the Box class. The section on exceptions
will discuss exception classes.
The Truck Class Template
Since a truck can carry any kind of cargo, the truck will be implemented
as a class template.
Our truck has the following minimum attributes. Other private attributes may
be added if you find them helpful in your implementation
- Its capacity as read from the command line
- Its current location
- The number of miles driven
- A list of the current contents of the truck
Our truck supports the following public oprerations.
No other public methods are permitted. You may define any
private methods you wish. In these definitions, "T" is the
template parameter.
The return types and constness of the methods have been purposely omitted.
- Construction of an empty truck. A newly created empty truck
has a user specified capacity (default capacity is 25),
has been driven zero miles and begins its journey at a location
specified as "Warehouse".
- Destruction of a truck.
- Load( const T& item);
Load an item into the truck. Duplicate items are permitted.
- Unload( const T& item);
Unload the specified item from the truck. If the item to be unloaded
is one of a set of duplicates, unload any one of the duplicates.
- Drive( const string& where, int miles);
Move the truck to the new location.
In addition, you must implement the overloaded operator<<
for the truck as a non-member friend function. This operator will
output the truck's current location, miles driven, and a list
of items currently in the truck.
The Sack Class
The Sack class has just two attributes -- its contents
(a case-sensitive string) and its weight in pounds. (a positive integer).
A sack is uniquely identified by its attributes.
The Sack supports the following public operations.
No other public methods are permitted. You may define any
private methods you wish. The return types and constness of the
methods have been purposely omitted.
- Construction of a Sack with user specified contents and weight.
By default, a new Sack contains one pound of Air.
- Destruction of a Sack.
- operator==( const Sack& sack); to determine if two
sacks are identical.
- operator!=( const Sack& sack); to determine if two
sacks are different.
In addition, you must implement the overloaded operator<<
for the Sack as a non-member friend function. This operator will output
both attributes of the sack on a single line.
The Box Class
The Box
class has the following minimum attributes which may be assumed
to be positive integers -- length, width, height, and weight in pounds.
A Box is uniquely identified by its attributes.
The Box class supports the following public operations.
No other public methods are permitted. You may define any
private methods you wish. The return types and constness of the
methods have been purposely omitted.
- Construction of a box with user specified attributes.
By default, a new box has length, width, height and weight = 1.
- Destruction of a box.
- operator==( const Box& box); to determine if
two boxes are identical (e.g. have all the same attributes).
- operator!=( const Box& box); to determine if
two boxes are different.
In addition, you must implement the overloaded operator<<
for the Box as a non-member friend function. This operator will output
all attributes of the box on a single line, clearly identifying the weight.
Exceptions
At a minimum, your project must detect the following exception
conditions and throw an exception. You may choose to create
and throw an exception class, or throw a string with an appropriate
message.
- An attempt to load a box or sack into a truck that is full
- An attempt to unload a box or sack from a truck that is empty
- An attempt to deliver a non-existent box or sack
Exceptions should also be thrown for any other exceptional conditions
which may occur in your particular implementation.
The Command File
The command file has the following format. Blank lines should be
ignored. Any non-blank line may be assumed to be properly formatted
and all data may be assumed to be valid.
- DRIVE-BOX <destination> <miles>
DRIVE-SACK <destination> <miles>
Tells the truck to drive to the given "destination"
(a string). The distance to the destination is given by "miles",
which is a positive integer. For example: DRIVE-BOX Boston 120
or DRIVE-SACK Washington 42
- LOAD-BOX <box attributes>
LOAD-SACK <sack attributes>
Tells the truck to load the specified box/sack onto the truck.
If the truck is full, an appropriate error message is displayed.
- DELIVER-BOX <box attributes>
DELIVER-SACK <sack attributes>
Tells the truck to deliver the specified box/sack at the
current location. If the box/sack is not on the truck or the truck is empty,
an appropriate error message should be displayed.
- PRINT
Tells both trucks to print their current inventory, current location
and the total number of miles driven so far.
The attributes of the box for the LOAD-BOX and DELIVER-BOX commands are separated by
white space and occur in the following order: length, width, height, weight.
The attributes of the sack for the LOAD-SACK and DELIVER-SACK commands are separated by
white space and occur in the following order: contents (a string with no
embedded spaces), weight in pounds.
Sample Output
Your program must produce two required outputs
- Each time a command is read from the command file
the command and its parameters must be displayed.
- Each time the PRINT command is encountered, the current location,
current mileage and list of current contents of both trucks must be printed.
The attributes of each box and sack must be displayed.
This sample output was NOT produced by processing any command file.
It is intended solely to provide an example of an acceptable output format.
Any similar format is allowed as long as all required data is provided.
Be sure the appropriate function is doing the appropriate output.
linux1[5]% Proj5 50 40 command1.dat
Processing File: command1.dat
Cmd: PRINT
BOX TRUCK
The truck's current locations is: Warehouse
The truck has driven 0 miles so far
The truck is empty
SACK TRUCK
The truck's current locations is: Warehouse
The truck has driven 0 miles so far
The truck is empty
Cmd: LOAD-BOX 2 3 4 9
Cmd: LOAD-BOX 2 4 5 12
Cmd: LOAD-SACK corn 50
Cmd: LOAD-BOX 3 5 7 9
Cmd: PRINT
BOX TRUCK
The truck's current locations is: Warehouse
The truck has driven 0 miles so far
The truck's contents
2 x 3 x 4 box weighing 9 lbs
2 x 4 x 5 box weighing 12 lbs
3 x 5 x 7 box weighing 9 lbs
SACK TRUCK
The truck's current locations is: Warehouse
The truck has driven 0 miles so far
The truck's contents
50 pound sack of corn
Cmd: DRIVE-SACK Baltimore 30
Cmd: DRIVE-BOX Catonsville 10
Cmd: DELIVER-BOX 2 4 5 12
Cmd: PRINT
BOX TRUCK
The truck's current locations is: Cantonsville
The truck has driven 10 miles so far
The truck's contents
2 x 3 x 4 box weighing 9 lbs
3 x 5 x 7 box weighing 9 lbs
SACK TRUCK
The truck's current locations is: Baltimore
The truck has driven 30 miles so far
The truck's contents
50 pound sack of corn
Cmd: DELIVER-BOX 3 5 7 10
Error: There is no such box
Hints, Suggestions and Free advice
- Mr. Frey's public directory for this project is
/afs/umbc.edu/users/d/e/dennis/pub/CMSC202/proj5
- Use incremental development
- Use a simple approach to reading the file. Using getline( ) is NOT recommended. A simple approach also makes EOF detection easier.
- Visit the discussion board
- Since Truck.H and Truck.C are templates, there are special
considerations
- Remember to guard both files
- Remember to #include Truck.C at the end of Truck.H
- DO NOT list Truck.o as an object file in your makefile; Truck.C gets
included in Proj5.C (via Truck.H) and compiled as part of Proj5.C
Project Design Assignment
Since we have specified the class interface, there is no design
assignment for project 5.
Project Make File
You are required to submit a make file with this project.
The grader should simply need to type "make Proj5" and your project should compile
and link to an executable called Proj5. The grader should also have the ability
to do a "make clean" and "make cleanest."
See the
make tutorial for more help on make and makefiles.
Project Grading
The grade for this project will be broken down as follows. A more detailed
breakdown will be provided in the grade form you receive
with your project grade.
This list should not be considered comprehensive, but rather a suggested list
of things you should consider before turning in your project.
85% - Correctness -- 85%
- Have you verified the command line and its parameters?
- Are all appropriate member functions "const"?
- Are all return values being returned in the appropriate way (by value, by reference,
by const reference)?
- Are all function parameters being passed into the function in the appropriate
way (by value, by reference, by const reference)?
- Does your output meet all output requirements? Is all output produced
by the appropriate function?
- Do all appropriate functions have the proper PreConditions? If so, are all
PreConditions handled?
- Are you throwing exceptions wherever required?
- Are all exceptions being caught and handled properly? I.e. being thrown
by the appropriate methods, caught by the caller and dealt with appropriately?
- Did you remember to delete all dynamically allocated memory (if any)?
- Are your classes implemented using appropriate OOP principles -- encapsulation
and abstraction.
- Does your implementation maximize code reuse?
- Does your Truck load/unload/print duplicate items correctly?
15% - Coding Standards
Your code adheres to the
CMSC 202 coding standards as discussed
and reviewed in class.
Project Submission
You should submit the following files.
- Proj5.C -- the main project file
- Truck.H and Truck.C -- class template definition and implementation
for the Truck class
- Box.H and Box.C -- class definition and implementation of the Box class
- Sack.H and Sack.C -- class definition and implementation of the Sack class
- Other .C and .H files as needed (e.g. Proj5Aux.H and Proj5Aux.C)
- Your makefile
Submit as follows:
submit cs202 Proj5
A complete guide to
submit tools is available on the course website.
More complete documentation for submit and related commands can be found
here.
Remember -- if you make any change to your program, no matter how
insignificant it may seem, you should recompile and retest your program before
submitting it. Even the smallest typo can cause compiler errors and a reduction
in your grade.