A stack is a useful tool for reversing a list of items. Pushing items onto a sta
ID: 3695694 • Letter: A
Question
A stack is a useful tool for reversing a list of items. Pushing items onto a stack and then popping them off results in the items being returned in the reverse order from which they were pushed. Use the following class for this assignment.
public class PalindromeRunner
{
public static void main(String[] args)
{
PalindromeTester pt = new PalindromeTester();
if (pt.isPalindrome("bob"))
{
System.out.println("bob is a palindrome");
}
else
{
System.out.println("bob is not a palindrome");
}
if (pt.isPalindrome("amanaplanacanalpanama"))
{
System.out.println("is a palindrome");
}
else
{
System.out.println("amanaplanacanalpanama is not a palindrome");
}
if (pt.isPalindrome("abcdefghijklmnopqrstuvwxyz"))
}
System.out.println("abcdefghijklmnopqrstuvwxyz is a palindrome");
}
else
{
System.out.println("abcdefghijklmnopqrstuvwxyz is not a palindrome");
}
}
}
You will need to build the PalindromeTester class and provide it with a with the boolean isPalindrome(String s) method that returns true if the string s is a palindrome, otherwise it returns false. Populate the class with a Stack<Character> object. Push each character of the passed string onto the stack. Java will autobox each char into a Character object.
Explanation / Answer
class PalindromeTester { public boolean isPalindrome(String s) { Stack stack = new Stack(); for(char c : s.toCharArray()){ stack.push(c); } String s2 = ""; int size = stack.size(); for(int i=0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.