Question:

Using either “ArrayStack” to write a Java program to decide if an input string containing the character ‘$’ exactly once is in the following set.

L = {An$Bn: where An = AA … A (there are n copies of A), Bn = BB … B (there are n copies of B, and n ≥ 0. When n = 0, both An and Bn are empty strings}.

Good input/output is $ or A$B, or AA$BB...
Wrong input/output is A$ or AA$B or A$BB or a$b...


Thank you for your time and your help!

This is the method that I need to modify to do the above. It currently reverses a string. I dont even know where to start though!

Java Code:
 public static void main(String[] args) {

	String cmd;

	cmd = JOptionPane.showInputDialog("B for displaying in reversal order; ");

	     if (cmd.equals("B"))
     {
     	String str10 = JOptionPane.showInputDialog(
         	"enter a string and I will use the stack to display it in "
         	+ "reversal order");

            ArrayStack s10 = new ArrayStack(200);
          for (int k = 0; k < str10.length(); k++)
             s10.push(new Character (str10.charAt(k)));

          while(!s10.isEmpty())
     	  {
     	  	 Character ch10 = (Character)(s10.top());
     	     System.out.println(ch10.charValue());
     	     s10.pop();

     	   }
      }

    }
}


This is all of the code.

Java Code:

import javax.swing.JOptionPane;


public class ArrayStack1 {

    public static void main(String[] args) {

	String cmd;

	cmd = JOptionPane.showInputDialog("B for displaying in reversal order; ");

	     if (cmd.equals("B"))
     {
     	String str10 = JOptionPane.showInputDialog(
         	"enter a string and I will use the stack to display it in "
         	+ "reversal order");

            ArrayStack s10 = new ArrayStack(200);
          for (int k = 0; k < str10.length(); k++)
             s10.push(new Character (str10.charAt(k)));

          while(!s10.isEmpty())
     	  {
     	  	 Character ch10 = (Character)(s10.top());
     	     System.out.println(ch10.charValue());
     	     s10.pop();

     	   }
      }

    }
}

 interface StackInterface

{
  public void push(Object item) throws StackOverflowException;
  // Effect:         Adds item to the top of this stack.
  // Postcondition:  If (this stack is full)
  //                    an unchecked exception that communicates
  //                    'push on stack full' is thrown
  //                 else
  //                    item is at the top of this stack.

  public void pop() throws StackUnderflowException;
  // Effect:         Removes top item from this stack
  // Postconditions: If (this stack is empty)
  //                   an unchecked exception that communicates
  //                   'pop on stack empty' is thrown
  //                 else
  //                   top element has been removed from this stack.

  public Object top() throws StackUnderflowException;
  // Effect:         Returns a reference to the element on top of this stack
  // Postconditions: If (this stack is empty)
  //                    an unchecked exception that communicates
  //                   'top on stack empty' is thrown
  //                 else
  //                   return value = (top element of this stack).

  public boolean isEmpty();
  // Effect:         Determines whether this stack is empty.
  // Postcondition:  Return value = (this stack is empty)

  public boolean isFull();
  // Effect:         Determines whether this stack is full.
  // Postcondition:  Return value = (stack is full)
}


//public
class StackOverflowException extends RuntimeException
{
  public StackOverflowException()
  {
  }

  public StackOverflowException(String message)
  {
    super(message);
  }
}


//public
class StackUnderflowException extends RuntimeException
{
  public StackUnderflowException()
  {
  }

  public StackUnderflowException(String message)
  {
    super(message);
  }
}


class ArrayStack implements StackInterface
{
  private Object[] stack;           // Array that holds stack elements
  private int topIndex = -1;        // index of top element in stack

  // Constructors
  public ArrayStack()
  {
    stack = new Object[100];
  }

  public ArrayStack(int maxSize)
  {
    stack = new Object[maxSize];
  }

  public void push(Object item)
  // Adds an element to the top of this stack
  {
    if (!isFull())
    {
      topIndex++;
      stack[topIndex] = item;
    }
    else
      throw new StackOverflowException("Push attempted on a full stack.");
  }

  public void pop()
  // Removes an element from the top of this stack
  {
    if (!isEmpty())
    {
      stack[topIndex] = null;
      topIndex--;
    }
    else
      throw new StackUnderflowException("Pop attempted on an empty stack.");
  }

  public Object top()
  // Returns the element on top of this stack
  {
    Object topOfStack = null;
    if (!isEmpty())
      topOfStack = stack[topIndex];
    else
      throw new StackUnderflowException("Top attempted on an empty stack.");
    return topOfStack;
  }

  public boolean isEmpty()
  // Checks if this stack is empty
  {
    if (topIndex == -1)
      return true;
    else
      return false;
  }

  public boolean isFull()
  // Checks if this stack is full
  {
    if (topIndex == (stack.length - 1))
      return true;
    else
      return false;
  }
}