November 26, 2003                                                                                                               Queens College

CS211                                                                                                                                     Instructor: John D. Donath

Test #2

 

Note: This test is open book/open notes.  This means that you can bring in any written or printed reference material for the exam.  However, use of any electronic device during the exam is strictly forbidden.  Communication of any sort with a third party in any manner during the exam will be regarded as cheating.

 

Directions:  Answer all questions by writing the appropriate code, or by sorting the array in the appropriate manner, in the space provided, directly on the test paper.  Only answers written on the test paper will be graded.  If you need extra space, you are allowed to use the back of the exam paper; however, you should make a clear indication on the front so that the graders know to look on the back. 

 

Upon receiving the examination paper and an index card, please write your student ID and test ID on every page of the exam.  Do not write your name on the exam paper.  An Index card will be distributed along with the exam.  Please write your name, testID and studentID on the index card.  These will be collected within the first five minutes of the exam.  You will receive 5% credit for following these directions.

 

Part I (40%):

For this question, we will be dealing with an inheritance hierarchy for a class called Utility, that implements accounts for customers with the given utility.  There are many different kinds of utilities, these include Electric, Gas, Water and Phone, among others.  Every Utility has in common the fact that a customer has a 14 digit account number (implemented as an array of characters, not an int).  Also, every customer has a balance that they owe the company.  For any utility, there is a function bill which uses information in a subclass (and in the superclass, see the individual descriptions below) to calculate how much the customer owes, and returns this amount.  Another function common to all utilities is pay which accepts an amount as its parameter, and reduces the balance by that amount.  It should also have an accessor function for the account number.

 

One subclass of Utility is Electric.  This class should store, in addition to the information it inherits from the superclass, the price per kilowatt hours (or price for short).  It should also store how much the customer used this month (or rather, unbilled electricity).  It should have mutator functions to set the amount of Kilowatt hours the customer used this month, and the price.  The bill function defined for this class should take the amount of electricity that the customer used and multiply it by the price.  It should add this to the balance stored in the superclass and return the total balance.  It should also zero out the amount of kilowatt hours the customer used, since customers would get upset if they were billed twice for the same electricity.

 

Another subclass of Utility is Water.  Water is billed at a steady rate to all customers, irrespective of how much they used.  Therefore, this class should store the monthly amount.  It should have a mutator function for this variable.  Its bill function should apply a 1.5% penalty to the amount the customer owes (unpaid balance), and add the monthly rate to the balance.  It should return the total balance.

 

Note:  You should answer both questions on the next page, and 1 out 2 questions on page 3.

 


 

A:  Write the .h file for the class Utility.  Note that it is not necessary to write comments (14%).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

B:  Write .h file for the class Water.  Note that it is not necessary to write comments (13%)


C:  Choose 1 of the following 2 questions.  Circle the one that you are implementing (13%)

 

I.  Implement the function bill written in the class Water.

 

II.  Write a function that accepts as its parameter an array of pointers to different utility accounts, and its length.  It prints out every account number, and how much each customer owes.   For each account, it also solicits from the user a payment, and applies that amount as a payment to the account.
Part II (45%):

For this question, you will be asked to write pieces of a template class called Arrays.  This class holds in it a growable array of pointers to any type, and another array containing the length of each array that the array of pointers is pointing to.  For example, an array of pointers to arrays of the following floating point numbers:

Arrays:                   Length:

{ 1.3, 4.1}               2

{ 3.1, 3.8}               2

{2.3, 2.1, 9}            3

 

In addition to the information described above, this class contains the following functions:

1.  Insert – receives a pointer to an array, its length and a location.  Stores these values into the class at the location given by the last parameter.

2.  getArrayAt – receives as parameters and integer, and a reference to an integer.  Returns a pointer to the array indexed by the first parameter, and sets the second parameter equal to the array’s length.

3.  remove – removes the array at the index passed to it by the parameter.

 

All three of these functions throw an Exception of type index if an index which is out of bounds is passed to it.

 

It may contain some other functions besides those mentioned above.

 

A:  Write the .h file for this class (14%)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

B: Write the destructor for this class (4%)
C:  Write the remove function defined in the .cpp file for this class.  Note that you are not allowed to make use of any loops in this function (ie. you should use a recursive approach to receive full credit) (14%)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D:  Write the copy constructor for this class (10%)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Part III (10%)

A:  Show how the following array will look like after each pass of insertion sort:

22,           4,             28,           6,             9

 

 

 

 

 

 

 

 

B:  How many passes will there be?