Computer Science 210 Lab 3: Exercising and Designing Classes
Due: February 16, 1998

Objectives and Overview: This lab provides an opportunity to exercise two important classes, the BitArray class and the String class. It also suggests how you can design your own class, the StringList class, that will be useful in solving a scheduling problem. This scheduling problem is a generalization of the INDEPENDENT SELECTION problem on page 51 of your text.

Because this lab requires a more sustained programming effort than the first two, you will have two weeks to complete it. During the first week, you should complete and hand in your answers to Parts 1 and 2, leaving the second week free to concentrate on Parts 3 and 4.

You are also encouraged to work in teams to complete the programming parts of this lab.

Part 1 - Build and Run the BitMain Program

The program files BitMain.cpp, BitArray.cpp, and BitArray.h are provided in the Courses -> CS210 (Tucker) -> source folder, for your use. Make copies of these files on the desktop for yourself and build a C++ project named Lab3.µ, as shown below:

The program BitMain.cpp generates random numbers to estimate the number of items from among N alternatives that will be left unselected after each of N people selects K items at random. It uses the BitArray B to identify the bits that are selected by a random number generating function Randint, as discussed in class.

  1. Run this program to be sure that it works, using input values of N = 100000 and K = 2. Your results should be similar (though not identical) to the results shown in Figure 2.6 on page 53 of your text. What is the output?
  2. Now run this program for N = 22 and K = 4, to simulate the survey that we conducted in class last week where the 22 students in class each selected 4 different time slots "at random." What is the output?
  3. The following are some of the results of a hand tabulation performed by Damon Lauder, indicating the number of students who chose each of the 22 time slots (each time slot is identified by its index in the list, from 0 to 21. The row Selected? gives 0 or 1, depending on whether or not someone selected it (0 means false, 1 means true).
  4. Time slot 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
    Selected? 0  0  1  1  1  1  1  1  1  0  1  1  1  1  1  1  1  1  0  1  1  1

    Modify the BitMain.cpp program so that it displays its output in this fashion. That is, it displays all N entries in the BitArray B, not just the number that are still empty.

Part 2 - Examine and Exercise the String Class

The String class interface is shown on page 80 of your text. It is a very useful class for storing and manipulating character strings. The program Stringtest.cpp is provided on the CS210 (Tucker) server to provide a program that tests the functions in this class. This program also tests C++ functions for reading input data from text files rather than from the keyboard.

To run this program, build a project called Lab3.µ on the desktop. Now drag copies of the following files from the CS210 (Tucker) -> source folder your Lab3 project folder and add all of these, in addition to Stringtest.cpp itself, to your project.

String.h
String.cpp
Exception.h
Exception.cpp

Now drag a copy of the file "schedule" from the CS210 (Tucker) folder into your Lab3 project folder. This file contains the data for the time slots and student preferences that you created last week in class. Now make and run this program.

  1. What is the type of value stored in the variables S1, S2, and S3?
  2. What is the role of the array T in this program?
  3. What is the role of S2 and S3 in the second half of the program?
  4. Give an example statement from this program that shows a use of the overloaded operator [].
  5. Give another example statement that shows a use of the overloaded operator =.
  6. Give another example statement that shows the use of a String constructor.

Part 3 - Design a New Class called StringList

You have some experience with linked lists of integers. Here, we propose that a new class, called StringList, be defined for linked lists of Strings. The implementation strategy may be the same as for linked lists of ints, except that new class interface StringList.h and implementation StringList.cpp files need to be created.

The public members of this new class should include:

StringList() - constructor creates a new StringList variable, initially empty
insert(String s) - inserts a new String s at the end of the linked list
display() - displays the strings in all the individual nodes in the list
length() - returns the length (number of nodes) of the list

The private members of this class should include a declaration for the node structure, pointers to the First and Last elements in the linked list, and an integer variable N that contains the number of nodes in the list. This suggests the following private declarations:

struct Node {
String Value;
Node * Next;
};
Node * First;
Node * Last;
int N;
  1. Complete the class interface file StringList.h for this new class. For now, include only the public and private members discussed above. Don't forget to start with the line
    1. #include "String.h"
    so that you can use the functions in the String class.
  2. Complete the class implementation file StringList.cpp for this new class.

These functions should be tested using some simple statements in a main program before you proceed to Part 4. In your main program, don't forget to start with the line

#include StringList.h

and be sure to add the StringList.h and StringList.cpp files to your project before running.

Part 4 - Exercise and Utilize the StringList Class

Here is a new problem. Suppose we want to write a program that develops and displays a list of the actual time slots, and with each time slot, the first names of all students who selected it. For instance, if Dara, Alexios, and Nick selected time slot MWF9:30, then the display should include the following line:

MWF8:30 Dara Alexios Nick

Since each time slot is a string, as is the first name of each student, we can see that the StringList is an appropriate data structre for storing the names of time slots and students efficiently. Moreover, since the number of students selecting different time slots will vary widely, a very flexible data structure is required. Here is a linked list of strings (a StringList) that represents the above sample information.

Now we need to develop this information for all the time slots, not just MWF8:30. This suggests an array of StringLists, each one beginning with a different time slot. The above StringList would appear in the 3rd entry in that array for the data file schedule, preceded by a StringList for the time slot MW8:00 and followed by a StringList for the time slot MWF9:30. That is, the following array declaration should be near the beginning of your program:

StringList * TimeSlots = new StringList[n];

where n is the total number of time slots in the input data file (22 in our case). Now we can refer to the ith time slot as TimeSlots[i-1] (remember, array indexing begins at 0), and insert a new String, say S, into it by saying TimeSlots[i-1].insert(S).

With this information, you should design and complete this program (call it lab3yourname.cpp), using the StringList, String, and Exception classes to facilitate the coding.

Lab 3 Deliverables:

On February 9, turn in your answers to Parts 1 and 2 of this lab, including a hardcopy listing of your modified BitMain.cpp program.

On February 16, submit to the Drop Box the class interface, class implementation, and main program for Parts 3 and 4. Don't forget to rename theseprogram filesby affixing your name to them. For example, if I were submitting a program file, it would be called lab3atucker.cpp, not just lab3.cpp.

Also, hand in hardcopy listings of these program files with your name(s) on them. Each one should contain sufficient commentary documentation for me to understand its overall design and logic.