UMBC CMSC 202, Computer Science II, Spring 1999, Sections 0101, 0102, 0103, 0104

Project 2: A Collection of Strings

Due: Monday March 15, 1999

Pseudocode Due: Week of March 1

See Project Notes


Objective

The objectives of this project are 1) to practice writing and testing classes in C++, 2) to practice using the new and delete operators, and 3) to use quicksort to sort a collection of strings.


Background

Character strings are important data items in a wide variety of applications. Because "string" is not a built-in data type in C, it is common to use a String class in C++ to overcome some of the shortcomings of strings in C, as well as to provide additional functionality and programmer convenience. In this project, you will begin by writing a simple class to encapsulate string data and string operations.

Collections of strings are also frequently required. Classes that represent a collection of objects are called collection or container classes. As a second step in this project, you will write a container class for strings.

Class header files are provided, but you will need to add the documentation for member functions. You will also need to design and write a test program to throroughly test the String class and its member functions.


Assignment

  1. Begin thinking about this project early. Draw pictures and write pseudocode. To help motivate you to do this, 10 points of this project will be earned by turning in pseudocode for the following functions:

    Your pseudocode will be collected in the discussion sections during the week of March 1. It may be printed by computer or handwritten. Be sure your name and section number appears. You will lose points if your section number is missing. No pseudocode accepted after March 5.

  2. Write a class called String. The class body is given below. Put the class body in String.H and implement the member functions in String.C. Note that Length() is an inline function, so it will be defined in String.H. class String { private: char* _cptr; int _length; public: String(); String (char* str); String (const String& str); ~String(); inline int Length(); void Display(); String& operator= (const String& str); String operator+ (const String& str); int operator== (const String& str); int operator!= (const String& str); int operator< (const String& str); int operator> (const String& str); int operator<= (const String& str); int operator>= (const String& str); }; Here are some notes and hints on some of the String member functions:

  3. Write a test program to test all of the member functions of your String class. Your main() function should be defined in a source file called TestString.C. The statements in your test program should be clearly commented to indicate where each member function is being tested.

  4. Write a class called StringCollection. This will be a container class that does not allow duplicates. The class body is given below. Put the class body in StringCollection.H and implement the member functions in StringCollection.C. Note that Count() is an inline function, so it will be defined in StringCollection.H. class StringCollection { private: int _count; String* _array; void Quicksort (String a[], int i, int j); inline void Exchange (String b[], int i, int j); int Partition (String a[], int low, int high); public: StringCollection(); ~StringCollection(); int Add (const String& str); inline int Count(); int Contains (const String& str); void Sort(); void Display(); };

    Here are some notes and hints on some of the StringCollection member functions:

  5. Put the main() shown below in a file called Proj2.C. Use this to test your StringCollection class. The executable should be called Proj2. int main() { const int num_words = 13; char* words[num_words] = {"virtual", "class", "operator", "try", "friend", "this", "delete", "private", "inline", "template", "friend", "const", "new" }; StringCollection some_keywords; for (int i = 0; i < num_words; ++i) { String temp (words[i]); if (! some_keywords.Add (temp)) { cout << words[i] << " is a duplicate, not added" << endl; } } some_keywords.Display(); some_keywords.Sort(); cout << "After sort: " << endl; some_keywords.Display(); return 0; }

  6. Adapt your makefile for this project so that your test program for the String class is an additional target. The name of the executable should be TestString, and the following command line should make it: make TestString


Implementation Issues

  1. In the String class, the memory for the character array must be dynamically allocated, with no predefined limit on the length of the string.

  2. Your StringCollection class should use a dynamically allocated array of String objects. There should be no limit on the number of String objects that can be stored in the collection. You will have to decide on an allocation strategy. It does not have to efficient, but make sure that it works! Here are three possibilities:

  3. You must have quicksort coded in your project. You may not use qsort() from the C run-time library.

  4. For this project, you should submit a makefile and six source files named as follows:


Extra Credit

There are many features and functions that could be added to the classes in this project, providing enhanced functionality and improved efficiency. Later in the course, you will also learn a better alternative to the Display() functions, namely overloading the output (insertion) operator. Here are three improvements you can make for extra credit. Each is worth 5 points; you many do any, all, or none.


Last Modified: 3 Mar 1999 16:46:11 EST by Alan Baumgarten, abaumg1@cs.umbc.edu

Back up to Spring 1999 CMSC 202 Section Homepage