Question: Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if

Stack help. I need help with my lab assignment.

Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored.

The main() method in the application StackMain uses the overloaded constructor to declare several objects and initialize them to various phrases to test your program for correctness. For each test phrase, the application outputs a message about whether the phrase is a palindrome or not (based on the value returned by evalPalindrome()) the original phrase, and the phrase in reverse order (using the value of reversePhrase changed by evalPalindrome().)

For this lab assignment, implement the evalPalindrome() method, as defined in the Javadoc comments found inside the class definition above. The method you write MUST make use of ArrayStack to reverse the order of the original phrase and to determine if the phrase is a palindrome or not. In other words, the method you write must call the methods in the ArrayStack class to reverse the order of the original phrase and to determine if it is a palindrome.

Note that a library named cctype is included in Palindrome.h. This library contains methods like toupper(), tolower(), isalpha(), and more, which you may find useful. You may also need methods in the cstring, and string libraries. For more information on these libraries, check Appendix H in the textbook (P805-808).

Only the javadoc comments and the body of the evalPalindrome() method should be modified. No other modifications to the included files should be made.

The program MUST output the result as the following:

This phrase IS a palindrome!

Original phrase: kayak

Reverse phrase: kayak

This phrase is NOT a palindrome.

Original phrase: Read dear

Reverse phrase: raed daeR

This phrase IS a palindrome!

Original phrase: Madam, I'm Adam

Reverse phrase: madA m'I ,madaM

This is NOT a palindrome.

Original phrase: The End

Reverse phrase: dnE ehT

This phrase IS a palindrome!

Original phrase: 2Racecar2

Reverse phrase: 2racecaR2

StackInterface.h

#ifndef _STACK_INTERFACE #define _STACK_INTERFACE

template class StackInterface { public: /** Sees whether this stack is empty. @return True if the stack is empty, or false if not. */ virtual bool isEmpty() const = 0; /** Adds a new entry to the top of this stack. @post If the operation was successful, newEntry is at the top of the stack. @param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */ virtual bool push(const ItemType& newEntry) = 0; /** Removes the top of this stack. @post If the operation was successful, the top of the stack has been removed. @return True if the removal is successful or false if not. */ virtual bool pop() = 0; /** Returns the top of this stack. @pre The stack is not empty. @post The top of the stack has been returned, and the stack is unchanged. @return The top of the stack. */ virtual ItemType peek() const = 0; }; // end StackInterface #endif

ArrayStack.h

#ifndef _ARRAY_STACK

#define _ARRAY_STACK

#include "StackInterface.h"

const int MAX_STACK = 50;

template

class ArrayStack : public StackInterface

{

private:

ItemType items[MAX_STACK]; // Array of stack items

int top; // Index to top of stack

public:

ArrayStack(); // Default constructor

bool isEmpty() const;

bool push(const ItemType& newEntry);

bool pop();

ItemType peek() const;

}; // end ArrayStack

#include "ArrayStack.cpp"

#endif

Palindrome.h

#include #include #include "ArrayStack.h" using namespace std;

#ifndef PALINDROME_H #define PALINDROME_H class Palindrome { private: /** * stores the letters in the string while it is being evaluated * to determine if it is a palindrome */ ArrayStack letters;

/** * original phrase to evaluate to determine if it is a palindrome */ string phrase;

public: /** * Default constructor. Initializes expression to an empty string. */ Palindrome ();

/** * Overloaded constructor that initializes the phrase. * @param - newPhrase - original phrase tested to determine if it is a * palindrome */ Palindrome (string newPhrase); /** * Evaluates the phrase to determine if it is a palindrome. Uses * a stack to reverse the order of the letters in the phrase and * to determine if the original phrase is a palindrome. A palindrome * is a sequence of letters that reads the same forward and backward; * however, all spaces and punctuation are ignored. * @return - true if phrase is a palindrome; false otherwise * @param reversePhrase - orginal phrase in reverse order, including * all puncutation and spaces */ bool evalPalindrome (string& reversePhrase); }; #endif

ArrayStack.cpp

#include // For assert

#include "ArrayStack.h" // Header file

template

ArrayStack::ArrayStack() : top(-1)

{

} // end default constructor

// Copy constructor and destructor are supplied by the compiler

template

bool ArrayStack::isEmpty() const

{

return top < 0;

} // end isEmpty

template

bool ArrayStack::push(const ItemType& newEntry)

{

bool result = false;

if (top < MAX_STACK - 1) // Does stack have room for newEntry?

{

top++;

items[top] = newEntry;

result = true;

} // end if

return result;

} // end push

template

bool ArrayStack::pop()

{

bool result = false;

if (!isEmpty())

{

top--;

result = true;

} // end if

return result;

} // end pop

template

ItemType ArrayStack::peek() const

{

assert(!isEmpty()); // Enforce precondition

// Stack is not empty; return top

return items[top];

} // end peek

// End of implementation file.

Palindrome.cpp

#include "Palindrome.h" using namespace std;

Palindrome::Palindrome () { phrase = ""; }

Palindrome::Palindrome (string newPhrase) { phrase = newPhrase; } // After completing this method, submit the entire .cpp // file as your solution for Lab 5. See method description in // Palindrome.h

bool Palindrome::evalPalindrome (string& reversePhrase) { //Place code for lab here------------------------------- }

StackMain.cpp

#include #include #include #include #include #include "Palindrome.h" using namespace std;

void testForPalindrome(string origPhrase);

int main (void) { const int NUM_PHRASES = 5; string allPhrases[NUM_PHRASES] = {"kayak", "Read dear", "Madam, I'm Adam", "The End", "2Racecar2"}; for (int i = 0; i < NUM_PHRASES; i++) testForPalindrome(allPhrases[i]); return EXIT_SUCCESS; }

void testForPalindrome(string origPhrase) { string reversePhrase;

Palindrome phrase(origPhrase); if (phrase.evalPalindrome(reversePhrase)) { cout << "This phrase IS a palindrome!" << endl; } else { cout << "This phrase is NOT a palindrome." << endl; } cout << "Original phrase: " << origPhrase << endl; cout << "Reverse phrase: " << reversePhrase << endl << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!