CMSC 202 Spring 2001

Project 4

Palindromes


Assigned  23 April 2001
Due  6 May 2001


Objectives

This project will allow you to design your own classes, use class templates and use exceptions.  You will also continue
to use composition (aggregation) in your class design.  You will be using the Stack, Queue and List ADTs discussed
in class.  As usual, you will also be creating a makefile for your project.

Description

    An interesting phenomenon in the English language is the existence of sentences, words and phrases (SWPs) that read the same forwards and backwards (if you ignore capitalization, punctuation, special characters (dash, apostrophe, etc) and spaces).  These sentences, words and phrases are called "palindromes".  Perhaps the most famous palindrome is "Madam, I'm Adam".  Mr. Frey's favorite palindrome is (you guessed it) "Bob".  Any single letter words ("a", "I") are examples of trivial palindromes.

    Your project will read SWPs from a text file (one SWP per line, no more than 50 characters) and determine whether or not each SWP is a palindrome.  Your program will output each SWP read from the file followed by the phrase "is a palindrome" or "is not a palindrome".  If the SWP is not a palindrome, your output will indicate the rightmost and leftmost characters that do not match, proving that it is not a palindrome.  See the sample output below.

    If this were not a course in object oriented programming using C++, you could write a simple function using loops that determine if a given string is a palindrome or not (if you can't, don't tell anyone).  You're not going to have it so easy.  You are required to create a class named SWP which contains a line read from the file. The SWP ADT includes a method named isPalindrome( ).  That method uses a Stack and a Queue to determine if the the SWP is a palindrome according to the following algorithm

  1. simultaneously push all the characters of the SWP onto a stack and enqueue them onto a queue (ignoring punctuation symbols, special characters and whitespace (spaces, tabs, newlines, etc.).
  2. simultaneously pop a character from the stack and dequeue a character from the queue.  If the characters don't match (ignoring upper/lower-case), then the SWP is not a palindrome.
  3. If all the characters are popped/dequeued, and all match (ignoring case) then the SWP is a palindrome.
Your Stack and your Queue must be class templates and must use a List (composition) which is also a class template.  You may choose to implement your list as either an array or as a linked list.  If you implement it as a linked list, you will also be creating a template for the linked list's Node class.  You can start by modifying your linked list code from project 3 (make it a template, add necessary special insert/remove functions), or you may find some code in the text helpful.

    Your Tasks:

  1. Write Proj4.C that contains main( ), reads the file, etc.....
  2. Implement the  SWP class. Remember that your class definition should be in your .H file and the implementation in your .C file. THIS IS NOT a class template.
  3. Implement the List class as a template (List<T>).  You may use either an array implementation or a linked list implementation -- your choice.
  4. Implement the Stack class as a template (Stack<T>). Your stack MUST use a List<T> as its data representation.
  5. Implement the Queue class as a template <Queue<T>).  Your queue MUST use a List<T> as its data representation.
  6. Create a makefile that compiles all necessary files (NOT your class templates) and creates an executable named Proj4.
  7. Submit  the required files

ADT Definitions

The following ADTs provide the public interface for each class. You may write whatever private methods you feel are necessary. Any function/method prototypes which are given below are intentionally incomplete -- you must decide the complete function signature for any function or class method you write.  All data representations in all classes must be private.  No public data is allowed.  All classes must throw an exception when a run-time error is detected.

The SWP ADT

    The SWP class represents a Sentence, Word or Phrase.  It supports the following operations. Your data representation may assume a maximum SWP size of 50 characters.

The Stack ADT

    The Stack ADT was described in class.  Your stack must be implemented as a class template. Don't forget to create exception classes to be thrown for any run-time errors the Stack may detect. The Stack supports ONLY the following public operations.
Your Stack's data must be stored in a List.

The Queue ADT

    The Queue ADT was described in class.  Your queue must be implemented as a class template. Don't forget to create exception classes to be thrown for any run-time errors the Queue may detect. It supports ONLY the following public operations.
Your Queue's data must be stored in a List.

The List ADT

    The List ADT described in class and used for Project 3 is insufficient for this project.  In addition to the operations previously described for project 3, your List class must support the "special" insert and remove functions necessary to support the Stack's push/pop and the Queue's enqueue/dequeue operations.  You may also want to declare additional constructor(s).  Your List class must be implemented as a class template, but may be either an array or linked list implementation.  If you choose a linked list implementation, you will also need a Node class template.  If you choose an array implementation, you must you dynamic memory allocation for the array.
    Don't forget to create exception classes to be thrown for any run-time errors the List may detect.
 

 The Node ADT

    The node is used only by the List class when the list is implemented as a linked list.  It is a class template. Because it used only by the List class, it has no public interface.  Instead, the List class is declared to be a "friend" of the Node class and directly accesses the private methods and data.  The Node class supports only the "Big-4".

The text file

    The text file will contain an unknown number of lines of text.  Each line of text will be no more than 50 characters long.  Each line in the file is a separate SWP (Sentence, Word or Phrase).  Blank lines may appear (by accident) anywhere in the file.  Your program should recognize them and ignore them.
    A sample text file can be found in Mr. Frey's public directory
        /afs/umbc.edu/users/d/e/dennis/pub/CMSC202/Proj4/proj4.txt
A plethora of palindromes can be found at  http://www.polymorf.org/palindromes.html if you choose to create your own text file for testing.


Requirements and Restrictions


 

Hints and Reminders


Sample Output

    This sample output was created by hand and not generated by any program.
Your output does not have to match exactly, but should print each SWP on a separate line and indicate whether or not the SWP is a palindrome or not.  If not, use the ^ character on the next line to indicate the leftmost and rightmost nonmatching characters which prove that the SWP is not a palindrome.

linux3[1]% Proj4 proj4.txt

CMSC 202 Project 4 -- Palindrome testing
File: proj4.txt

    "A Santa at NASA" is a palindrome
    "Able was I, ere I saw Elba." is a palindrome
    "ABCCA" is not a palindrome
      ^ ^
    "Don't nod" is a palindrome
    "evil olive" is a palindrome
    "XYZYX" is a palindrome
    "Won't lovers revolt now?" is a palindrome
    "qwertxytrewq" is not a palindrome
          ^^
linux3[2]%


Makefiles

    The "make" utility is used to help control projects with large number of files.  It consists of targets, rules, and dependencies.
For this project, simply typing the command make or make Proj4 at the Unix prompt will compile and link your source files with Proj4.C and produce a new executable called Proj4.  Typing make clean will remove any extraneous files in your directory such as .o files,  and core files.  Typing make cleanest removes all .o files, core, Proj4 and backup files made by your editor.
    See a documented makefile for more information, or see the Project page for a link to even more info about make


What to Submit

    For this project, you must submit the following files. It is your responsibility to submit all files necessary to create your executable so that the graders can simply type "make".
  1. SWP.C and SWP.H -- the sentence/word/phrase implementation and header files Note that this is NOT a template class.
  2. Your makefile -- it must be named "makefile" or "Makefile".  The graders will simply type "make" and your files must compile and link all source files as appropriate
  3. Proj4.C -- your main project driver
  4. Stack.C and Stack.H -- the stack class template and Stack related exception classes
  5. Queue.C and Queue.H -- the queue class template and Queue related exception classes
  6. List.C and List.H -- the List class template and List related exception classes
  7. Node.C and Node.H -- the Node class template and Node related exception classes (required only if the List is implemented as a linked list)
  8. If you create your own exception header files and source code files, be sure to submit them as well.