Computer Science 210 Lab 4: Exercising and Designing Classes
Due: October 13, 1998

Objectives and Overview: This lab provides an opportunity to exercise an important class, 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 program will combine components of the first three labs:  arrays, lists, and classes.    The goal of the lab is to create a program which could be useful in scheduling.  For instance,  if we were to have oral examinations in this class then it would be necessary to schedule each student for a time slot.  One way to do this is to collect the times when different students would be available.  Doing the actual scheduling requires building a data structure which represents this information in some efficient way.  When you are done with this lab you should have a program that reads schedule information from a file, creates a data structure based upon that information, and prints out an organized representation of the information.

Part 1 - Examine and Exercise the String Class

C++ does not contain any standard string variable.  Instead, many compilers come with their own string classes, or there are standard libraries available with string classes.  For this lab I have provided an example string library in the file String.h in the source folder.  It is a very useful class for storing and manipulating character strings. The program Stringtest.cpp on the CS210 (Chown) server provides 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.

Your goal in Part 1 of the lab is to get comfortable with the String Class as well as learn how to do rudimentary file operations.

To run this program, build a project called Lab4.µ on the desktop. Now drag copies of the following files from the CS210 (Chown) -> source folder your Lab4 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 (Chown) folder into your Lab4 project folder. This file contains  data for  time slots and student preferences. 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 2 - 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 3. 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 3 - 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 Kim, Thurston, and Wasif selected time slot 9:00, then the display should include the following line:

9:00 Kim Thurston Wasif

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 9:00. This suggests an array of StringLists, each one beginning with a different time slot. The above StringList would appear in the 2nd entry in that array for the data file schedule, preceded by a StringList for the time slot 8:30 and followed by a StringList for the time slot 9: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 (19 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 4 Deliverables:

Submit to the Drop Box the class interface, class implementation, and main program for Parts 2 and 3. 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 lab4echown.cpp, not just lab4.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.