Question

This lab will give you a practice with both queue and stack ADTs. In this work,...

This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on.

Sample Output:

Please enter a string of characters: abba

The given string is a palindrome.

Want to examine another string? (y/n): y

Please enter a string of characters: 11223311

The given string is not a palindrome, since the symbol at position 3 from the left is different from the symbol at position 3 from the right.

Want to examine another string? (y/n):n

Bye!

// This class provides method to test if a string is a Palindrome using Stack/Queue ADTs.
// Provide more inputs here.
import java.util.Stack;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
class Palindrome {
// This is a main method to test checkPalindrome method
public static void main(String[] args) {
String inputString = new String("");
Scanner in = new Scanner(System.in);
do {
// please put your code to test checkPalindrome method here
} while ( inputString.equals("y") && inputString.length() == 1 );
System.out.print("Bye!");
}
// This is checkPalindrome method. It checks if an input string is Palindrome or not.
// It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds
// a different value.
// Pre-Condition: string must not be null.
// Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where
// a difference found.
public static int checkPalindrome(String strValue) {  
Stack<Character> stack = new Stack<Character>();
Queue<Character> queue = new LinkedList<Character>();
  
// check if string is null. If it is null, return a -1
  
// normalize the string values to lower case, remove spaces
strValue = strValue.toLowerCase().replaceAll("\\W", "");

// store data on stack/queue adts first

// loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference

return indexVal;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my implementation.

Pelase rate my answer if it helped you!!

import java.util.Stack;

import java.util.Scanner;

import java.util.Queue;

import java.util.LinkedList;

public class Palindrome {

   // This is a main method to test checkPalindrome method

   public static void main(String[] args) {

       String inputString = new String("");

       Scanner in = new Scanner(System.in);

       do {

           // please put your code to test checkPalindrome method here

           System.out.print("Please enter a string of characters: ");

           inputString = in.nextLine();

          

           int index = checkPalindrome(inputString);

          

           if(index == -1) {

               System.out.println("Input string is null");

           }else if(index > 0) {

               System.out.println("The given string is not a palindrome, since the symbol at position "+index

                       + "from the left is different from the symbol at position "+index+" from the right.");

           }else{

               System.out.println("The given string is a palindrome.");

           }

          

           System.out.print("Want to examine another string? (y/n): ");

           inputString = in.nextLine();

       } while ( inputString.equals("y") && inputString.length() == 1 );

       System.out.print("Bye!");

   }

   // This is checkPalindrome method. It checks if an input string is Palindrome or not.

   // It returns 0 if a string is a Palindrome. Otherwise, it returns a position of a character where it finds

   // a different value.

   // Pre-Condition: string must not be null.

   // Post-Condition: Return 0 if input string is a Palindrome. Return a positive number indicate the location where

   // a difference found.

   public static int checkPalindrome(String strValue) {

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

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

       // check if string is null. If it is null, return a -1

       if(strValue == null)

           return -1;

       // normalize the string values to lower case, remove spaces

       strValue = strValue.toLowerCase().replaceAll("\\W", "");

       // store data on stack/queue adts first

       for(int i=0; i<strValue.length(); i++) {

           char c = strValue.charAt(i);

          

           stack.push(c);

           queue.add(c);

       }

       // loop: comparing, retrieving text, terminate loop if stack is emptied or found a difference

      

       int indexVal = 0;

      

       while(!stack.isEmpty()) {

          

           if(stack.peek() != queue.peek()) {

               return (indexVal+1);

           }

          

           indexVal++;

           stack.pop();

           queue.poll();

       }

       return 0;

   }

}

| Quick Access 哈歇8/ Scala Studentsin R Proble @ JavadoB. Declara Ap search貝Consol × Progre dl Remote TestNG <terminated> Pali

Add a comment
Know the answer?
Add Answer to:
This lab will give you a practice with both queue and stack ADTs. In this work,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Ticketing service (Queue)

    LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to the peopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input is:Zadie Smith Tom Sawyer You Louisa Alcott -1the output is:Welcome to the ticketing service...  You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!TicketingService.javaimport java.util.Scanner; import java.util.LinkedList; import java.util.Queue; public class TicketingService {    public static void main (String[] args) {       Scanner scnr = new Scanner(System.in);...

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Given main(), complete the program to add people to a queue

    4.14 LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to thepeopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input isZadie Smith Tom Sawyer You Louisa Alcottthe output isWelcome to the ticketing service...  You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!Code that I have been...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a firs...

    Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a first-in-first-out (FIFO) data structure. For the Queue container, we will implement a program that simulates a buffer. The simulation needs a function that randomly generate number. And the buffer simulation starts with no value in the buffer. At the start of simulation, the menu should ask user: how many rounds will be simulated. the percentage chance to put a randomly generated...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Java, please. Work based on the code above. DESCRIPTION: The purpose of this question is offload...

    Java, please. Work based on the code above. DESCRIPTION: The purpose of this question is offload processing from the main method to a static helper method. You will be given the main method. Do not alter this code. Your goal is to write a new method called oddOneOut, which will accept a String, and print it in out every other letter. For example, if the String was "abcdefghijk", the oddOneOut method will return "acegik" METHOD INPUT: • A string METHOD...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT