Assignment 11 will be the construction of a program that tak
Assignment #11 will be the construction of a program that takes an input string, and check each string separated by \'#\' if it is a palindrome or not. A string is a palindrome if it reads the same backward or forward, for example, \"kayak\" or \"level\".
Your program needs to read an input string that contains substrings separated by \'#\', for instance, \"kayak#level#sea\"
The method check() in the PalindromeChecker class needs to check if each of substrings in such input string is a palindrome using Stack and Queue. (Note that since there is no Queue class in the java library, you will need to use LinkedList class that implements Queue interface.)
Assignment11 class
This class displays a menu for the palindrome checking. If a user enters \"E\", then it asks to enter a string containing some substrings to check if they are palindromes. You can assume that such input string will contain at least one non-empty string with at least one character. This class is given by the instructor.
PalindromeChecker class
The PalindromeChecker class contains a constructor to set up an initial configuration.
You need to complete the following method.
Please see the PalindromeChecker.java for more details.
public void check()
You need to write the check method that reads every character of the input string until the end of the string or it encounters the character \'#\', and store every character into the stack, charStack, and into the queue, charQueue. Then it should remove every character from charStack and charQueue to see if the two characters are same.
If all characters are same, then it is a palindrome.
As an example, if the substring is \"kayak\", then the following output should be shown:
The input [k, a, y, a, k] is a palindrome
It should make use of toString( ) method of the Queue (LinkedList class).
If they are not same, then the substring is not palindrome, and it should print out the message:
The characters \'a\' and \'b\' do not match\ \
where \'a\' in this case is the character removed from the queue, and \'b\' is the character popped from the stack. Note that \'a\' and \'b\' are just examples, so it should display the first set of two characters that are different.
The following is an example, if the input substring is \"sea\", then the output should be:
The input [s, e, a] is not a palindrome
The characters \'s\' and \'a\' do not match
Please see the PalindromeChecker.java file for more details.
Requirements:
You need to implement this method using an object of the Stack class and LinkedList class (as a Queue) in java.util package.
Error Handling
Your program is expected to be robust enough to pass all test cases. You may use the Scanner class, but are not limited to it, to handle your user input.
Class Diagram
PlaindromeChecker.java (Please complete this file)
Solution
import java.io.*;
public class Assignment11 {
public static void main(String[] args) throws IOException {
char input1;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.println(\"What action would you like to perform?\");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) {
// matches one of the case statements
switch (input1) {
case \'E\': // Enter Problem parameters
System.out.print(\"Please enter a string to check:\ \");
String inputString = stdin.readLine().trim();
// create an object of PalindromeChecker using the input
// string
PalindromeChecker checker = new PalindromeChecker(
inputString);
checker.check();
break;
case \'Q\': // Quit
break;
case \'?\': // Display Menu
printMenu();
break;
default:
System.out.print(\"Unknown action\ \");
break;
}
} else {
System.out.print(\"Unknown action\ \");
}
} while (input1 != \'Q\' || line.length() != 1);
}
/** The method printMenu displays the menu to a user **/
public static void printMenu() {
System.out.print(\"Choice\\t\\tAction\ \" + \"------\\t\\t------\ \"
+ \"E\\t\\tEnter String\ \" + \"Q\\t\\tQuit\ \"
+ \"?\\t\\tDisplay Help\ \ \");
}
}
import java.util.Stack;
import java.util.LinkedList;
public class PalindromeChecker {
private Stack<Character> charStack; // stack can contain characters
private LinkedList<Character> charQueue; // queue can contain characters
private String inputString;
// Constructor to initialize member variables
public PalindromeChecker(String inputString) {
charStack = new Stack<Character>();
charQueue = new LinkedList<Character>();
this.inputString = inputString;
}
// The check method checks if an input string is a palindrome or not,
// and prints its result.
public void check() {
boolean success = true; // success indicates whether palindrome or not
boolean done = false; // it is used to stop the while loop
int i = 0; // i wil be used as an index of the inputString
String currWord = inputString.toLowerCase();
// pushes the letters into a stack 1 by 1
for (i = 0; i < currWord.length(); i++) {
// convert the char to a Char object (Symmetry)
Character a = new Character(currWord.charAt(i));
// push the object ot the top of the stack
charStack.push(a);
}
// same operation as above, but for the queue
for (i = 0; i < currWord.length(); i++) {
Character a = new Character(currWord.charAt(i));
charQueue.add(a);
}
i = 0;
Character queueFront = \' \';
Character stackTop = \' \';
while (!done && (charQueue.isEmpty() == false)) {
/**** 1. ADD Your Code Here ****/
// until the end of inputString or it encounters \'#\' character
// take each character in inputString from left and add it to
// charStack and charQueue
queueFront = (Character) charQueue.remove();
stackTop = (Character) charStack.pop();
// prints out the substring extracted from the input string using
// queue\'s toString method
// System.out.print(\"The input \" + charQueue.toString());
/**** 2. ADD Your Code Here ****/
// until the charQueue or charStack becomes empty
// remove a character from each of charStack and charQueue, and
// check if they are same.
// If they are different, then print out the approproate message
// (\" is not a palindrome\ \")
// and also print which first set of two characters are different
// (\"The characters \' \' and \' \' do not match\ \ \") -- note that you
// need to print
// two such characters inbetween \' \'
// Also set success to false, so that the following \"if\" statement
// will be skipped
// if the front of the queue is not equal to the top of the stack,
// return false
if (queueFront.equals(stackTop) == false) {
success = false;
} else {
success = true;
}
if (success == true)
;
else {
break;
}
// i reaches the end of string, it is done processing the
// inputString
if (i == inputString.length()) {
done = true;
break;
} else {
i++;
success = true; // set it back to true for the next substring to
// check
}
} // end of while loop
if (success == true)
System.out.print(\"[\" + inputString + \"] is a palindrome\ \ \");
else {
System.out.print(\"[\" + inputString + \"] is not a palindrome\ \ \");
System.out.println(\"The characters \'\" + queueFront + \"\' and \'\"
+ stackTop + \"\' do not match\ \ \");
}
} // end of check( ) method
} // end of PalindromeChecker class
OUTPUT:
Choice Action
------ ------
E Enter String
Q Quit
? Display Help
What action would you like to perform?
E
Please enter a string to check:
ABA
[ABA] is a palindrome
What action would you like to perform?
E
Please enter a string to check:
ABC
[ABC] is not a palindrome
The characters \'a\' and \'c\' do not match
What action would you like to perform?
E
Please enter a string to check:
Kayak
[Kayak] is a palindrome
What action would you like to perform?
E
Please enter a string to check:
Srinivas
[Srinivas] is not a palindrome
The characters \'r\' and \'a\' do not match
What action would you like to perform?
Q




