Programming Stacks Minimal Submitted Files You are required

Programming Stacks

Minimal Submitted Files

You are required, but not limited, to turn in the following source files:

Assignment11.java (You do not need to modify this file.)
PalindromeChecker.java -- complete this file.

Requirements to get full credits in Documentation

The assignment number, your name, StudentID, Lecture number, and a description of each class/file need to be included at the top of each file/class (in this assignment, you might have only one file/class, but in future there will be more than one file.)

A description of each method is also needed.

Some additional comments inside of methods (especially for a \"main\" method) to explain code that are hard to follow should be written. You can look at Java programs in the text book to see how comments are added to programs.

New Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Stacks

Program Description

Class Diagram:

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.)

Instruction:

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.

Assignment 11 - Do not edit

Palindrome Checker

Solution

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

     while (!done)
      {

         /****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
            charStack = new Stack<Character>();
            charQueue = new LinkedList<Character>();
            for (int j=i; j<inputString.length(); j++){
             if (inputString.charAt(j)==\'#\' || inputString.charAt(j)==\'\\u0000\'){
               break;
             }
             //System.out.print(\"The input \" + charQueue.toString());
             charStack.add(inputString.charAt(j));
             charQueue.add(inputString.charAt(j));

            }
          i += charStack.size();
         //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
          for(int k=0; k<charStack.size(); k++){
            if(charStack.pop() != charQueue.get(k)){
              success = false;
            }
          }
          if (success == true)
              System.out.print(\" is a palindrome\ \ \");
          else
              System.out.print(\" is not a palindrome\ \ \");
          //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

Programming Stacks Minimal Submitted Files You are required, but not limited, to turn in the following source files: Assignment11.java (You do not need to modif
Programming Stacks Minimal Submitted Files You are required, but not limited, to turn in the following source files: Assignment11.java (You do not need to modif
Programming Stacks Minimal Submitted Files You are required, but not limited, to turn in the following source files: Assignment11.java (You do not need to modif

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site