CS-1030 Software Design 2
Lab 4: File Text Search v2.0 - Dynamic Memory

Objectives

Assignment

This is a 2-week lab, with a milestone demonstration due within 1 week.

ln Lab 3, you wrote a console-based application that searched text files for all occurrences of a given string within the file. In this lab, your program will appear to work as before (identically, in fact) from the user's perspective, but you will internally modify your program according to a substantially different design of the internal components.

The UML diagram illustrating the skeletal structure of your modified application is shown below:

The FileTextSearcher class implements the attributes and methods in the UML class diagram; in addition:

the getNextSearchResult() method of FileTextSearcher is superceded by getSearchResults(). The getSearchResults() method, rather than being called repeatedly from within your main application, must only be called once. With this single call, this new method now returns a SearchResultsCollection* pointer. The SearchResultsCollection object referenced (pointed to) by the returned pointer actually points to a whole "linked list" of SearchResult objects, which your main application must iterate through in order to retrieve each individual result.

Your SearchResult class must be modified to chain, or point to, additional instances of SearchResult objects, via a private SearchResult* pointer. The last SearchResult object in the chain will contain a NULL SearchResult* pointer called nextResult. Each time a SearchResult object is created, its constructor should initialize this pointer to NULL.

The FileTextSearcher class is the creator of the SearchResultsCollection object and all SearchResult objects that the collection contains. This implies that, somewhere in the search method, the new() operator is being invoked to create all those SearchResult objects. It also causes each new SearchResult object to be added to the SearchResultsCollection by calling the addSearchResult and passing a SearchResult* ponter to that method. (Note that the SearchResultsCollection object should append each new SearchResult object onto the end of its collection, rather than insert it at the beginning.) After creating these objects, your main application receives the pointer to the SearchResultsCollection via a call to the FileTextSearcher method getSearchResults().  Once it has this SearchResultCollection* pointer, your program can iterate through the linked list by following the pointers, beginning with a call to SearchResultCollection's getFirstSearchResult() method.

When your main application is done processing (reporting) the search results, it has to clean up - that is, it has to delete() all of the SearchResult objects in the chain, as well as the SearchResultsCollection object. Thus, while FileTextSearcher takes responsibility for creating the objects, in this case it does not bear the burden of disposing of them, and leaves that up to your main (or some other global function associated with your main). You have to do this cleanup in the correct order so that you don't "orphan" any objects and cause memory leaks; consider this aspect of the assignment very carefully. Note that when you invoke the delete() operator on a pointer (to an object), that object's destructor is automatically called.

Lab Activities

  1. Week 1: Partial implementation. Create the SearchResultsCollection class and modify the SearchResult class per the UML class diagram. In your main source module, surround your existing code with #if 0...#endif. This will exclude that code from compilation. In a new #if 1...#endif block, create a second main() function. This will be used for testing purposes.

    In this new main() function, dynamically create a new instance of a SearchResultsCollection object. Within a for() loop, create about 10 SearchResult objects. Supply "dummy" parameters to the SearchResult constructor to simulate search result information. As each SearchResult object is created, add it to the SearchResultCollection object.

    After adding these SearchResult objects to the SearchResultCollection object, use a while() loop to iterate through all SearchResult objects in the collection, and print out the contents of each object to the console in the order in which they were found.

    After displaying the results, destroy (delete) all SearchResult objects and the SearchResultCollection object.
     
  2. Week 2: Implement the complete functionality according to the preceding description. Revert from your test program back to your real program. Implement the creation of the SearchResultCollection and SearchResult objects within the FileTextSearcher class. Here of course, FileTextSearcher will be creating "real" instances of SearchResult objects as a result of the file search. In the end, your program will produce exactly the same output as your previous lab 3 program did.

Milestone demonstration

This is a 2 week lab, but you must demonstrate the partial implementation no later than during the second lab 4 session;  preferably, you will complete this part in the first lab session.

You must also submit your partial implementation (but no report) according to these instructions:
  1. Navigate to your VS .NET project directory (see this note on configuring the workspace).
  2. You should have a subdirectory under your VS .NET project directory with the name of the project you used for this lab. 
  3. Upload your submission through WebCT (assignment "Lab 4 Part 1"), selecting only the .cpp, .h, .sln, and .vcproj files from this directory. Upload each file separately; do not zip files.
  4. Enter the overall time you spent on this lab into the FAST system (for week 5).

Completed Program and Lab Report

The lab report and completed program is due by Tuesday, April 25 at 11pm.

You must demonstrate your finished, working program during the lab 5 session (Wednesday, April 26).

Submit your completed assignment following these instructions:
  1. Navigate to your VS .NET project directory (see this note on configuring the workspace).
  2. You should have a subdirectory under your VS .NET project directory with the name of the project you used for this lab. 
  3. Upload your submission through WebCT (assignment "Lab 4 Part 2"), selecting only the .cpp, .h, .sln, .vcproj, and .doc files from this directory. Upload each file separately; do not zip files.
  4. Enter the overall time you spent on this lab into the FAST system (for week 6).
Be sure to keep copies of all your files, in case something gets lost.

Your lab grade will be determined by the following factors:

Program quality Report quality