Assignment3 PalindromeStacksAndQueues
Assignment3 PalindromeStacksAndQueues
In this assignment, you will be implementing Stacks and Queues. You will then use stacks and
queues to test if words are palindromes, and use stacks to decompose phrases into words (to
find new palindromes).
● Implement MyStack (use the included MyLinkedList to store the stack elements)
○ Implements StackInterface
○ Throws a StackException if peek or pop are called on an empty stack
● Implement MyQueue (use MyLinkedList to store the stack elements)
○ Implements QueueInterface
○ Throws a QueueException if peek or dequeue are called on an empty queue
● Test your MyStack and MyQueue thoroughly
● In Palindrome.java
○ Implement stackToReverseString() using MyStack
○ Implement reverseStringAndRemoveNonAlpha() using MyStack
○ Implement isPalindrome(), which returns true if a word or phrase is a
palindrome, using MyStack and MyQueue
○ CHALLENGE: Implement explorePalindrome() which lists all possible
backwards decompositions of a phrase (e.g. “evil guns” => snug live“), a common
trick to make new palindromes (uses MyStack)
● Style requirements (NEW!)
○ Indent your code properly
■ There are several mainstream “styles” of indentation, pick one and be
consistent. EG: https://javaranch.com/styleLong.jsp
○ Name your variables with helpful and descriptive names
■ Whats a good variable name? Here is a guide
○ Add comments before every function, and in your code
■ Comments should say what you are trying to do and why
■ Consult this guide to commenting
Point breakdown
Stacks - 20 points
Queues - 20 points
Style - 15 points
Implement MyStack.java and MyQueue.java using the provided interfaces and exceptions.
Make sure you have implemented a public String toString() method on MyStack and
MyQueue that prints out the contents of the stack from the top down and prints out the queue
from the front to back. So, for example, after the following code:
MyStack stack = new MyStack();
MyQueue queue = new MyQueue();
stack.push("Hello");
queue.enqueue("Hello");
stack.push("big");
queue.enqueue("big");
stack.push("world");
queue.enqueue("world");
Is this a palindrome?
A palindrome is a word or phrase that reads the same backwards and forwards, if you ignore
punctuation and spaces. For example:
● A dog! A panic in a pagoda!
● ABBA
● Cigar? Toss it in a can. It is so tragic.
● Yo, bottoms up! (U.S. motto, boy.)
● Stressed was I ere I saw desserts.
(from http://www.palindromelist.net/palindromes-y/)
In this part of the assignment, you will be writing several functions in Palindrome.java to test
and create palindromes using your stack and queue implementations.
Expand palindromes
Which words could be added to make this a palindrome?
javac Palindrome.java && java Palindrome expand "an era live" "mug god"
an era live: evil a ren a
an era live: evil aren a
an era live: evil arena
mug god: dog gum
Functions to implement:
String stackToReverseString(MyStack): your
toString function in your stack class prints out the
stack in the order that things would be popped from
it. What if we want to turn it into a string in the
opposite order (the order that things were pushed
to it)?
Boolean isPalindrome(String)
Implement this function using both a stack and a queue.
To test if a string is a palindrome:
● Convert the string to lowercase (we don’t care about uppercase vs lowercase characters
being different)
● Create a new stack and queue.
● Enqueue and push each character (if it is alphabetic, we don’t want to look at white
space or punctuation)
● Pop each character from the stack and dequeue each character from the queue until
one is empty
● Notice how in our above functions, pushing and then popping from a stack reversed the
order. How will you use this to test whether this string is a palindrome?
void explorePalindrome(String)
This function lists all possible endings that would make this string a palindrome, e.g.:
javac Palindrome.java && java Palindrome expand "an era live" "mug god"
an era live: evil a ren a
an era live: evil aren a
an era live: evil arena
Now, most of the work will be done by a recursive helper function to decompose this new string
(“evilarena”) into words. Takes the original string, the reversed string, an index, and the current
stack of words we are building up
public static void decomposeText(String originalText, String
textToDecompose, int index, MyStack decomposition)
We have provided a function String[] getWords(String text, int index) that uses a
dictionary to find which words could be created from a string at a given index. For example
getWords("isawere", 0) could find the words “i” and “is”, getWords("isawere", 2) could
find the words (“a”, “aw” and “awe”).
A recursion step:
● If the index is at the end of the word, we are finished, print out the words (using
reverse print) and the original text.
● Else: Find the potential words at that index
● For each word:
● Push it to the stack
○ Recurse at the next index (not *just* i++)
○ If it was part of a correct solution, it will print itself out in a subsequent
recursion step (if it reaches a conclusion)
● Pop it from the stack
● Confused? See below for a visual explanation of this approach.
○ As usual, println statements may help you understand your code’s operation if
you get lost. Consider outputting the list of words from getWords, or printing out
the decomposition stack each time you push or pop from it. Just remove your
debug statements before turning in your code.