This is for a simple Java Program using stacks and queues. 4. Program: Write a p
ID: 3820724 • Letter: T
Question
This is for a simple Java Program using stacks and queues.
4. Program: Write a program that reads a line of text, changes each lowercase letter to uppercase, and places each letter both in a queue and onto a stack.The program should then ver- ify whether the line of text is a palindrome.
TEXT BELOW ARE CLARIFICATIONS:
We have completed our study of Stacks and Queues this week.
Our second "for credit" assignment will be an easy one using Stacks and Queues. From the textbook, do Programming Exercise #4 (page 483) EXCEPT for the following refinements and clarifications:
Write a class that contains a static isPalindrome method to report (as true or false) if a string passed as the parameter is a palindrome or not. Write another class (in a separate file) to test the method.
Your method should push each alpha character in the string onto a stack and also add it to a queue. Then, pop each character off of the stack at the same time as you remove one from the queue and compare them for equality. If they all pass, you have a palindrome (because the LIFO nature of the stack reverses the string while the FIFO nature of the queue does not).
Don't use any of the authors source code, instead, pick a stack and queue class from the Java 8 SE API. Notice that the Stack is a concrete class in Java, but the Queue is an interface (so you will need to choose one of the implementing, concrete Queue classes). Note also that Java 8 features generics, so you can make a stack of characters as follows:
Remember, the Java API for Stack is a bit different from that in the textbook so you will use peek, push and pop. Also, for your choice of Queue (perhaps the ArrayDeque or the LinkedList) you would likely use peek, add and remove.
As before, the testing class should use iteration to prompt the user for strings to evaluate, terminating on the entry of an empty string. Only the letters (alpha-characters) in the string are evaluated (spaces, punctuation and numbers are discarded while casing is ignored). Do all the work in the method, not the driver. The pre-condition for the method is only that the parameter is a valid string.
Name your implementation class PalindromeEvaluator and your testing class PalindromeTester (because that is what my script will look for - following all the instructions counts for points).
Also as before, make sure your testing method doesn't re-open your Scanner (or any other class you wrap around System.in) more than once (that would break the standard input, a general programming standard) and be sure your posted solution will compile at the command line with the Java 8 SE compiler. Make sure your output prints true or falsefollowed by the input string as in:
Thanks for your help!
Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
####################################
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class PalindromeEvaluator {
public static boolean isPalindrome(String input)
{
Queue<Character> q = new LinkedList<Character>();
Stack<Character> s = new Stack<Character> ();
char letter;
int i;
// iterating over input string
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i);
// if current character is digit or letter then push into stack and add into queue
if(Character.isLetter(letter)){
letter = Character.toLowerCase(letter);
q.add(letter);
s.push(letter);
}
}
// now pop from stack and dequeue from queue character by character and compare
while (!q.isEmpty( ))
{
if (q.poll() != s.pop( ))
return false;
}
return true;
}
}
####################################
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class PalindromeTester {
public static void main(String[ ] args) throws IOException
{
Scanner input = new Scanner(System.in);
// opening input file
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
// reading line by line
while((line = reader.readLine()) != null){
if(line.isEmpty())
break;
// checking whether current line is palindrome or not
System.out.println(PalindromeEvaluator.isPalindrome( line )+" : "+line);
}
reader.close();
}
}
/*
Sample run:
Was it a cat I saw?
true : Was it a cat I saw?
Madam
true : Madam
Car
false : Car
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.