Question

Hello, I need help with following question - Write a method to implement below question. You...

Hello, I need help with following question

- Write a method to implement below question. You can use either StackArrayBased or StackListBased to create the stack.

L= {ww':w is a string of characters w' = reverse (w)}

Note: The empty string, a string with less than 2 characters, or a string with an odd number of characters will not be in the languege.

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

Please find attached the java program which has stack implementation array based
Copy the below java function in file named Reverse.java
Run it as 'javac Reverse.java' and then java Reverse. Or you can copy paste the code in eclipse.

/* Reverse.java*/

import java.util.*;

class ArrayStack {
   private int maxsize;
   private char[] stackarray;
   private int top;
  
   public ArrayStack(int size) {
       maxsize = size;
       stackarray = new char[maxsize];
       top = -1;
   }
  
   public void push(char x) {
       this.top++;
       stackarray[top] = x;
   }
  
   public char pop() {
       return stackarray[top--];
   }
  
   public boolean isEmpty() {
       if (this.top == -1)
           return true;
       return false;
   }
  
   public boolean isFull() {
       if (this.top == maxsize -1)
           return true;
       return false;
   }
}
public class Reverse {

   public static void main(String[] args) {
       System.out.println("Enter the string");
      
       Scanner s = new Scanner(System.in);
       String input = s.next();
      
       if(input.length() < 2 || input.length() %2 != 0){
           System.out.println(input + " is not in L");
           System.exit(1);
       }
       ArrayStack stack = new ArrayStack(input.length() / 2);
      
       for(int i = 0; i < (input.length() / 2); i++) {
           char c = input.charAt(i);
           stack.push(c);
       }
      
       for(int i = input.length()/2; i<input.length(); i++) {
           char c = stack.pop();
           if (c != input.charAt(i)) {
               System.out.println(input + " is not in L");
               System.exit(1);
           }
                      
       }
      
       System.out.println(input + " is in L");

   }

}

Add a comment
Know the answer?
Add Answer to:
Hello, I need help with following question - Write a method to implement below question. You...
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
  • 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...

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

  • Could somebody please help.! Please DON't copy paste from other places and please read the question...

    Could somebody please help.! Please DON't copy paste from other places and please read the question and answer exactly what's asked. Thank you I much appreciate it. You are to write a program name MyArrayStack.java that create/build the ArrayStack data structure. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import any stack from the Java Library): 1. One default constructor that will create an...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    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 the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • I need help with this exercise. Thank you for your help Language is C++ 6. Write...

    I need help with this exercise. Thank you for your help Language is C++ 6. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd inte- ger is prime if it is not divisible by any odd integer less than or equal to the square root of the number.)

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • Hi, I need help with Javascript please. Question: Write a function named "tweets" that takes a...

    Hi, I need help with Javascript please. Question: Write a function named "tweets" that takes a string as a parameter and returns the number of tweets required to tweet the input to the world. Note: The maximum length for a single tweet is 280 characters. Thanks!

  • Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo...

    Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo Post fix ( infix) // Converts an infix expression to an equivalent postfix expression operatorStack = a new empty stack postfix = anew empty string while (infix has characters left to parse) nextCharacter =next nonblank character of infix switch (nextCharacter) { case variable: Append nextCharacter to postfix break case 'A' operatorStack.push (nextCharacter) break case '+ case '-' : case '*' : case '/' while...

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