CIS c++

profilerick07
hwp2.rtf

[INSERT TITLE HERE] 2

Running head: [INSERT TITLE HERE]

[INSERT TITLE HERE]

Student Name

Allied American University

Author Note

This paper was prepared for [INSERT COURSE NAME], [INSERT COURSE ASSIGNMENT] taught by [INSERT INSTRUCTOR’S NAME].

P ART I

1) Define the exception class MemoryAllocationException and then revise the definition of the method push in the class LinkedStack so that it throws this exception if it cannot allocate a new node.

2) Improve the palindrome-recognition algorithm C++ source code (shown below in blue), by adding the first length / 2 characters to the queue and then pushing the remaining characters onto the stack.

// Tests whether a given string is a palindrome.

isPalindrome(someString: string): Boolean

// Create an empty queue and an empty stack

aQueue = a new empty queue

aStack = a new empty stack

// Add each character of the string to both the queue and the stack

length = length of someString

for (i = 1 through length)

{

nextChar = ith character of someString

aQueue.enqueue(nextChar)

aStack.push(nextChar)

}

// Compare the queue characters with the stack characters

charactersAreEqual = true

while (aQueue is not empty and charactersAreEqual)

{

queueFront = aQueue.peekFront()

stackTop = aStack.peek()

if (queueFront equals stackTop)

{

aQueue.dequeue()

aStack.pop()

}

else

charactersAreEqual = false

}

return charactersAreEqual

3) Revise the parameterized constructor (shown below) to call the base-class’s constructor instead of MagicBox’s constructor.

/** @file MagicBox.h */

#ifndef _MAGIC_BOX

#define _MAGIC_BOX

#include "PlainBox.h"

template<class ItemType>

class MagicBox : public PlainBox<ItemType>

{

private:

bool firstItemStored;

public:

MagicBox();

MagicBox(const ItemType& theItem);

void setItem(const ItemType& theItem);

}; // end MagicBox

#include "MagicBox.cpp"

#endif

/** @file MagicBox.cpp */

template<class ItemType>

MagicBox<ItemType>::MagicBox()

{

PlainBox<ItemType>();

firstItemStored = false; // Box has no magic initially

} // end default constructor

template<class ItemType>

MagicBox<ItemType>::MagicBox(const ItemType& theItem)

{

firstItemStored = false; // Box has no magic initially

setItem(theItem); // Box has magic now

} // end constructor

template<class ItemType>

void MagicBox<ItemType>::setItem(const ItemType& theItem)

{

if (!firstItemStored)

{

PlainBox<ItemType>::setItem(theItem);

firstItemStored = true; // Box now has magic

} // end if

} // end setItem