October 22, 2003                                                                                                   Queens College

CS211                                                                                                                     Instructor: John D. Donath

Test #1

 

Note:  This test is open book/open notes.  This means that you are allowed to use references from textbooks, notes or other sources.  You are not allowed to refer to any electronic sources, or operate any electronic devices during the exam.  Any form of communication during the examination will be considered cheating.  You may use any paper that you like for scrap; however, only answers written on the examination paper will be graded.  Please do not staple or hand in additional sheets of paper.

 

Directions:  Please write the test ID and your student ID on every page of the test.  At the end of the test, you must hand in all the papers that you received.  Also, please write your name, testID and studentID on the index card that you received along with the test.  The index cards will be collected within the first 5 minutes of the examination.  Please do not write your name on the test paper.  You will receive 5 points for following these and all other directions.

Answer all the questions directly on the test paper, on the space provided.

 

You may use the following class on the exam.  You need not provide its implementation.  You may also use any library classes that you wish, so long as the appropriate library paths are included in your code:

/**

 * Keeps track of data about a package.  Data that we wish to store include who sent it, who it is being sent to, and a brief description

 * of its contents.

 *

 * @author:  John D. Donath

 */

class Package {

                public:

                                Package(); // Creates an instance of this Package where it is sent from nobody to nobody with no contents

                                Package(string f, string t); // Sets the from and to values to the parameters, but the contents get set to the

                                                                         // empty array

                                Package(string f, string t, string c);  // Sets the values in the class to those passed by the parameters.

                                int getFrom() const {return from;}

                                float getTo() const {return to;}

                                string getContents() const {return contents;}

                                void setContents(const string& str) { contents = str; }

                private:

                                string from;

                                string to;

                                string contents;

};

 

For this exam, you will be working with the class Mailbox, where a collection of packages can be stored.  In addition, we store the name of the owner of the Mailbox (as a string).  The number of packages in a Mailbox should be able to grow to a (theoretically) infinite size.  Different parts of the exam will implement different stages in the lifetime of Mailbox.  The following operations should be available for an instance of Mailbox.

                1.   Put a package into the Mailbox.

                2.   Empty out the Mailbox.  This should return an array of all the packages previously stored in the Mailbox, but current elements should no longer be stored in the Mailbox.

                3.  Ability to merge two Mailbox classes by using the + operator.  Note that the merged class should contain all the packages in both of the classes.

                4. Return all the packages in the Mailbox that were sent by a certain person (passed by a parameter), this function should also accept and set a reference parameter the number of packages sent by that person..

                5.  A function, clean, that will remove all packages from the Mailbox that are not addressed to the owner of the Mailbox.

                6.  Compare two collections using the == operator.*

                7.  Compare two collections using the != operator.*

                8.  An accessor function for the owner of the mailbox

*Note:  Two Mailbox classes are considered equal if they have the same packages in the same order.


Part I:

1.  Write the complete .h file for the class Mailbox.  Please provide the complete file including all necessary includes, macro definitions and function declarations.  You need not comment your code (30%).


Part II:

Implement the following functions that are in the .cpp file for the Mailbox class.  You need not implement the complete .cpp file.  You should implement only the functions that are listed. Write all your code on this page and the next page.

 

1.  The function that adds a package to the mailbox (10%).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.  The overload of the + operator (10%)


3.  The function that returns all the packages that were sent by a certain person (10%)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.  The overload of the = operator (10%)


Part III:

Implement the following function in the file that contains the main program (25%):

 

Write a function that receives as a reference parameter an instance of the class Mailbox, an array of packages, and an reference to an integer with the length of that array.  It should add to the instance of Mailbox that it receives all the packages in the array that are addressed to the owner of the Mailbox.  It should remove these from the array, and adjust the length accordingly.

 

For example, if I called the function in the following way:

                Mailbox mb;  Package d[10]; int i;

                // some more code here

                mailTo(mb, d, i);

It will place all packages addressed to the owner of the mailbox in the mailbox, removing them from the array d. 

(Note that the class string has a constructor that takes in a c-string as its only parameter.)