Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment I check if input is a palindrom by comparing a stack and que

ID: 3837967 • Letter: F

Question

For this assignment I check if input is a palindrom by comparing a stack and queue. I have the program being able to do that but I need a driver class for it. How would I go about doing that?

Here is the program for the code that checks if palindrome:

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

import java.util.Scanner;

public class PalindromeChecker

{

   public static void main(String[ ] args)

   {

       Scanner keyboard = new Scanner(System.in);

       String input;

      

      System.out.println("Please enter input to check if palindrome.");

      input = keyboard.nextLine( );

       if (isPalindrome(input))

       {

           System.out.println("Input is a palindrome.");

       }

       else

       {

           System.out.println("Input is not a palindrome.");

   }

   }

  

   public static boolean isPalindrome(String input)

   {

      Queue<Character> queue = new LinkedList<Character>( );

      Stack<Character> stack = new Stack<Character>( );

      char letter;

      int i;

  

      //each letter will be added onto stack and queue

      for (i = 0; i < input.length( ); i++)  

      {

            letter = input.charAt(i);          

            queue.add(letter);

            stack.push(letter);

      }

      //stack and queue will be compared

      while (!queue.isEmpty( ))  

      {

   if (queue.remove( ) != stack.pop( ))

   return false;

      }

      return true;

   }

   }

Explanation / Answer

Here are two classes.

PalindromeChecker.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class PalindromeChecker
{
public boolean isPalindrome(String input)
{   
Queue<Character> queue = new LinkedList<Character>( );
Stack<Character> stack = new Stack<Character>( );
char letter;
int i;
  
//each letter will be added onto stack and queue
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i);
queue.add(letter);
stack.push(letter);
}
//stack and queue will be compared
while (!queue.isEmpty( ))
{
if (queue.remove( ) != stack.pop( ))
return false;
}
return true;
}
}

PalindromeCheckerDriver.java

import java.util.Scanner;

public class PalindromeCheckerDriver {
public static void main(String[ ] args)
{
PalindromeChecker palindromeChecker = new PalindromeChecker();
Scanner keyboard = new Scanner(System.in);
String input;
  
System.out.println("Please enter input to check if palindrome.");
input = keyboard.nextLine( );

if (palindromeChecker.isPalindrome(input))
{
System.out.println("Input is a palindrome.");
}
else
{
System.out.println("Input is not a palindrome.");
}
keyboard.close();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote