Question

Stack help. I need help with my lab assignment. Complete a method for a class named...

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 ItemType>
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 ItemType>

class ArrayStack : public StackInterface<ItemType>

{

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 <string>
#include <cctype>
#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<char> 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 <cassert> // For assert

#include "ArrayStack.h" // Header file

template<class ItemType>

ArrayStack<ItemType>::ArrayStack() : top(-1)

{

} // end default constructor

// Copy constructor and destructor are supplied by the compiler

template<class ItemType>

bool ArrayStack<ItemType>::isEmpty() const

{

return top < 0;

} // end isEmpty

template<class ItemType>

bool ArrayStack<ItemType>::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<class ItemType>

bool ArrayStack<ItemType>::pop()

{

bool result = false;

if (!isEmpty())

{

top--;

result = true;

} // end if

return result;

} // end pop

template<class ItemType>

ItemType ArrayStack<ItemType>::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 <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
#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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//main.cpp

#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
#include "Palindrome.h"
using namespace std;

void testForPalindrome(string origPhrase);

int main (void)
{
const int NUM_PHRASES = 4;
string allPhrases[NUM_PHRASES] =
{"kayak",
"Read dear",
"Madam, I'm Adam",
"The End"};

for (int i = 0; i < NUM_PHRASES; i++)
testForPalindrome(allPhrases[i]);
  
return EXIT_SUCCESS;  
}


void testForPalindrome(string origPhrase)
{
string reversePhrase;
bool isPalindrome;
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;
}

===================================================================================

//Palindrome.cpp

#include "Palindrome.h"

#include <iostream>

using namespace std;

Palindrome::Palindrome ()

{

phrase = "";

}

Palindrome::Palindrome (string newPhrase)

{

phrase = newPhrase;

}

bool Palindrome::evalPalindrome (string& reversePhrase)

{

ArrayStack<char> theStack;

string current;

bool palin = false;

for(int i=0; i<phrase.length(); i++)

theStack.push(phrase[i]);

// pop each character of the stack back onto reversePhrase with a loop and using

for(int i=0; i<phrase.length(); i++){

current = theStack.peek();

theStack.pop();

// reversePhrase as an indexable array of characters

reversePhrase+=current;

}

//test for palindrome

cout<<"Phrase: "<<phrase<<endl;

cout<<"Reverse Phrase: "<<reversePhrase<<endl;

for (int i=0; i<phrase.length(); i++){

if(!ispunct(phrase[i]) && !isblank(phrase[i])){ // this logic is off.

//if not punctuation or blank

toupper(phrase[i]);

toupper(reversePhrase[i]);

if(phrase[i] != reversePhrase[i]){

palin = false;

return palin;

}

else

palin = true;

}

}

return palin;

}

=====================================================================================

//Palindrome.h

#include <string>
#include <cctype>
#include <iostream>
#include "ArrayStack.h"
using namespace std;

#ifndef PALINDROME_H
#define PALINDROME_H
class Palindrome
{
private:
ArrayStack<char> letters;
string phrase;

public:
Palindrome ();
Palindrome (string newPhrase);
bool evalPalindrome (string& reversePhrase);
};
#endif

===================================================================================

//StackInterface.h

#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE

template<class ItemType>
class StackInterface
{
public:
virtual bool isEmpty() const = 0;

virtual bool push(const ItemType& newEntry) = 0;
virtual bool pop() = 0;
virtual ItemType peek() const = 0;
}; // end StackInterface
#endif

==================================================================================

// ArrayStack.cpp

#include <cassert> // For assert

#include "ArrayStack.h" // Header file

template<class ItemType>

ArrayStack<ItemType>::ArrayStack() : top(-1)

{

}

template<class ItemType>

bool ArrayStack<ItemType>::isEmpty() const

{

return top < 0;

} // end isEmpty

template<class ItemType>

bool ArrayStack<ItemType>::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<class ItemType>

bool ArrayStack<ItemType>::pop()

{

bool result = false;

if (!isEmpty())

{

top--;

result = true;

} // end if

return result;

} // end pop

template<class ItemType>

ItemType ArrayStack<ItemType>::peek() const

{

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

// Stack is not empty; return top

return items[top];

} // end peek

// End of implementation file.

====================================================================================

//ArrayStack.h

#ifndef _ARRAY_STACK

#define _ARRAY_STACK

#include "StackInterface.h"

const int MAX_STACK = 50;

template<class ItemType>

class ArrayStack : public StackInterface<ItemType>

{

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

==================================================================================

sample output

CProgram Files Dev-CpplProjectl.exe This phrase IS a palindrome! Original phrase: kayak everse phrase kayak hrase: Read dear

Add a comment
Know the answer?
Add Answer to:
Stack help. I need help with my lab assignment. Complete a method for a class named...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • tem Type bool result faise i result true What method is this? a) pop0 b) peekO...

    tem Type bool result faise i result true What method is this? a) pop0 b) peekO c) default constructor d) copy constructor template < class Item Type> ItemType ArrayStack<item Type>:doSomething) const assert (lisEmptyO): return items[top]: What method is this? e) pop) f) peek) g) default constructor h) copy constructor C.Given this graphic of an array based stack. b) top i tems 2 o 20 30 MAX STACK-1 Array indices What would be returned by a call to the method peek0)...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT