Assignment11javaSolutionPlease follow the code and comments

Assignment11.java

Solution

Please follow the code and comments for description :

CODE :

a) Assignment11.java :

import java.io.*; // required imports for the file

public class Assignment11 { // class to run the code

public static void main(String[] args) throws IOException { // driver method
char input1; // required initialisations
String line = new String();

printMenu(); // call the method to print the options

InputStreamReader isr = new InputStreamReader(System.in); // read the data
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\ \ \");
}
}


b) PalindromeChecker.java :

import java.util.Stack; // required imports
import java.util.LinkedList;
import java.util.Objects;
import java.util.Scanner;

public class PalindromeChecker { // class to run the code

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

while (!done) {

//until the end of inputString or it encounters \'#\' character
//take each character in inputString from left and add it to charStack and charQueue
//prints out the substring extracted from the input string using queue\'s toString method
  
for (int m = 0; m < inputString.toCharArray().length; m++) {
charQueue.add(inputString.charAt(m));
charStack.push((Character) inputString.charAt(m));
}
System.out.print(\"The input \" + charQueue.toString());

//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
  
while (!charQueue.isEmpty()) {
Character a = charQueue.remove();
Character b = charStack.pop();
if (!a.equals(b)) {
success = false;
System.out.print(\" is not a palindrome\ \ \");
System.out.print(\"The character \'\"+a+\"\' and \'\"+b+\"\' do not match.\ \ \");
break;
}
}
if (success == true) {
System.out.print(\" is a palindrome\ \ \");
} else {
break;
}

//i reaches the end of string, it is done processing the inputString
if (i == inputString.length()) {
done = true;
} else {
i++;
success = true; //set it back to true for the next substring to check
}
} //end of while loop
} //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:
masm
The input [m, a, s, m] is not a palindrome

The character \'a\' and \'s\' do not match.

What action would you like to perform?
e
Please enter a string to check:
mushroom
The input [m, u, s, h, r, o, o, m] is not a palindrome

The character \'u\' and \'o\' do not match.

What action would you like to perform?
q

Hope this is helpful.

Assignment11.javaSolutionPlease follow the code and comments for description : CODE : a) Assignment11.java : import java.io.*; // required imports for the file
Assignment11.javaSolutionPlease follow the code and comments for description : CODE : a) Assignment11.java : import java.io.*; // required imports for the file
Assignment11.javaSolutionPlease follow the code and comments for description : CODE : a) Assignment11.java : import java.io.*; // required imports for the file

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site