Results 1 to 1 of 1
- 04-30-2012, 11:17 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
ArrayStack Question that I dont even know how to start it.
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; } }
Similar Threads
-
Help. i dont know where to even start.
By kiddCOWBOYY in forum New To JavaReplies: 21Last Post: 03-20-2012, 06:09 PM -
Last Question: Illegal Start of Expression
By miss_peaches in forum New To JavaReplies: 2Last Post: 05-12-2011, 06:08 AM -
Implementations of an ArrayStack class
By coyler_21 in forum NetBeansReplies: 5Last Post: 10-27-2010, 09:04 AM -
What forum should I use for a Web Start question?
By Malingo in forum Advanced JavaReplies: 2Last Post: 06-26-2010, 02:29 AM -
Hello! and I need help. I dont know were to start
By Fall0ut in forum New To JavaReplies: 10Last Post: 05-19-2010, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks