LAB3 (20pts) - The Text ADT

Purpose: In this lab you will explore the implementation and applications of a Text ADT, a simplified version of the STL’s string class. This lab is found at pg. 2 in your lab manual, which can be accessed through our Libraries --> Databases --> Safari Tech Books Online -->C++ Data Structures A Laboratory Course OR at this link.


Pre-lab: Implement methods for Text ADT.

1. Carefully read pages 2-9 in the lab manual. Besides telling you exactly what you need to do, the manual provides background information and helpful hints. Use their discussion of the methods to create brief pre- and post-conditions for all methods you are implementing. The pre and post documentation goes in .h to serve as documentation for the clients using your class. Some developpers put them in both .h and .cpp. In this course it is sufficient to put them in .h, but write them  before you implement the methods because they will help you with the implementation.

2. Create a project named Text ADT and populate it with the following files. Run your project. It should compile and run eventhough the methods in the .cpp file are not implemented yet; for now they each have a a dummy body to allow you to complie with no errors. You will work on implementing most of these functions as required below.

3. Implement (the first 8 methods of the Text ADT – from constructor to showStructure()) and test (tests 1-1 through 1-5) the Text ADT, following the steps in the Implementation Notes (pg. 5) and Testing (pg. 9) sections. Before the lab, get at least 80% of the test cases to work.

4. Use the Text Worksheets.pdf to help you with your work - if not handed by instructor, print this file and provide answers for the testing scenarios in the document.

5. Bring your Text class and Text Worksheets.pdf to lab.

6. Some additional info you might find useful follows.

‘h’

‘e’

‘l’

‘l’

‘o’

‘\0’

‘h’

‘e’

‘l’

‘l’

‘o’

‘\0’

‘x’

#include <cstring> //try <string> if cstring library is not found
...
char * str = “hello world”;   // null-term. array of chars
cout << strlen(str) << endl;  // prints 11

Text:: Text ( const Text &valueText )

// Copy constructor, creates a copy of valueText. Called whenever
//
//   1) a string is passed to a function using call by value,
//   2) a function returns a string, or
//   3) a string is initialized using another string -- as in the
//      declarations:
//                      Text str1("First"),
//                             str2 = str1;

{
    // Step 1: Allocate memory for buffer
    // Step 2: Copy the elements of valueText.buffer into buffer
    // Step 3: ??? (There’s one more thing to do.)
}

void Text::operator= ( const Text& other )

// Assigns other to a Text object.

{
    if (this != &other) {
      int rlen = other.getLength();   // Length of other

      if ( rlen >= bufferSize )       // If other will not fit
      {
         delete [] buffer;            // Release buffer and
         bufferSize = rlen + 1;             //  allocate a new
         buffer = new char [ bufferSize ];  //  (larger) buffer
      }

      strcpy(buffer, other.buffer);    // Copy other’s buffer
      bufferSize = other.bufferSize;  // Copy other’s bufferSize
    }
}


In-lab: Programming Exercise 1

1. If any test cases for your Text class are not working, you may ask the instructor for help.

2. Do Programming Exercise 1 from the lab manual. Begin with the starting code, LexicalAnalyzer.cpp, which handles file input for you. Note: make sure you do NOT have two main() methods in one project. (Temporarely, you could change one to something like main33()).

3. Other files you might find useful for this activity are

textio.cpp - code stubs for certain methods used in the lab

progsamp.dat - the input file for LexicalAnalyzer.cpp

subjects.txt - another sample input file

4. Two possible outputs for this activity are output_1 and output_2. When done with this activity, run your program with progsmap.dat as your input, and create a snapshot of your output. Name it ex1_YourLastName.png (or .jpg). You will submit this picture with your project.
Note: use the global path when inputing into your program the name of your text file (similarly, I used /Users/svisa/Desktop/TextADT/TextADT/progsmap.dat).


Post-lab: Programming Exercise 3

  1. Do Programming Exercise 3. You are recommended to start the post-lab during lab. It will take longer than the in-lab part. 
  2. Search the web for information on how to use strcmp to compare two character arrays.
  3. Follow the lab instructions for exercise three to test your implementation of the operators.
  4. A possible output for this activity is output_3. When done with this activity, run your program to test it as described in your labmanual, and take a snapshot of your output; name it ex3_YourLastName.png (or .jpg). You will submit this picture with your project.

Grading:

  1. Text Worksheet.pdf: 4pts
  2. Pre, post in .h files: 2pts (make sure you have them for ALL methods, not only the ones you wrote.)
  3. 8 methods (3 are given to you, 5 you are implementing): 10pts
  4. Progr. ex. 1: 2pts (1 pt is the picture)
  5. Progr. ex. 3: 2pts (1 pt is the picture)