Right now in my class we are dealing with array stack data structures. I have successfully put together an array stack class which creates a stack that holds strings(or in my case string elements). However the assignment that we are working on makes us test to see if the string has matching delimiter sets. I'm having trouble even thinking about how to approach this in writing a method for it.
2. Write a program that analyzes an expression for matching the delimiter set: () [] {}. For example the expression { 2 + [ a - ( b * f ) ] + e }; contains matching delimiters.
This is what I have so far.
public class TestStack{ public static void main(String[] args){ ArrayStack list = new ArrayStack(); System.out.println("Initializing stack."); list.initializeStack(); System.out.println("Done."); System.out.println("Testing the isEmptyStack method."); System.out.println(list.isEmptyStack()); System.out.println("Done."); System.out.println("Adding 2 StringElements to the stack."); list.push(new StringElement("{ 2 + [ a - ( b * f ) ] + e }")); list.push(new StringElement("{ 3 + ( 2 - 4 ) - 6 }")); System.out.println("Testing the top method."); StringElement stg = (StringElement)list.top(); System.out.println("The element from the top of the stack returned: " + stg.message); System.out.println("Done."); System.out.println("Testing isFullStack method."); System.out.println(list.isFullStack()); System.out.println("Done."); System.out.println("Testing the pop method."); list.pop(); //deletes the top element of the stack System.out.println("Deleted the top element of the stack using the pop method."); stg = (StringElement)list.top(); //prints out the new top element of the stack System.out.println("The element from the top of the stack returned: " + stg.message); System.out.println("All methods from ArrayStack class have been tested."); } }
public class ArrayStack{ //variable to store the max stack size private int maxStackSize; //variable to point to the top of the stack private int stackTop; //array of ref variables private DataElement[] list; //default constructor public ArrayStack(){ maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create array }//end default constructor //constructor with a parameter public ArrayStack(int stackSize) { if(stackSize <= 0) { System.out.println("The size of the array to implement " + "the stack must be positive."); System.out.println("Creating an array of size 100."); maxStackSize = 100; } else maxStackSize = stackSize; //set the stack size to //the value specified by //the parameter stackSize stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create the array }//end constructor //copy constructor public ArrayStack(ArrayStack otherStack) { copy(otherStack); }//end copy constructor private void copy(ArrayStack otherStack) // copy data only { list = null; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new DataElement[maxStackSize]; //copy otherStack data into this stack’s data for(int i = 0; i < stackTop; i++) this.list[i] = otherStack.list[i].getCopy(); }//end copy // a new Stack is not created //copy data from otherStack to this stack public void copyStack(ArrayStack otherStack) { if(this != otherStack) //avoid self-copy copy(otherStack); }//end copyStack public void initializeStack(){ for(int i =0; i<stackTop; i++){ list[i] = null; stackTop = 0; } } //end initializeStack public boolean isEmptyStack(){ return (stackTop==0); }//end isEmptyStack public boolean isFullStack(){ return (stackTop == maxStackSize); }//end isFullStack public void push(DataElement newItem) { if(isFullStack()) throw new StackOverflowException(); list[stackTop] = newItem.getCopy(); //add newItem at the //top of the stack stackTop++; //increment stackTop }//end push public DataElement top() { if(isEmptyStack()) return null; return list[stackTop - 1].getCopy(); }//end top public DataElement pop() { if(isEmptyStack()) return null; DataElement temp = list[stackTop - 1].getCopy(); stackTop--; //decrement stackTop list[stackTop] = null; return temp; }//end pop }
public abstract class DataElement{ public abstract boolean equals(DataElement otherElement); public abstract int compareTo(DataElement otherElement); public abstract void makeCopy(DataElement otherElement); public abstract DataElement getCopy(); }
public class StringElement extends DataElement{ protected String message; public StringElement(){ message = "Default String"; } public StringElement(String msg){ message = msg; } public boolean equals(DataElement obj){ if(obj == null) return false; else if (getClass() != obj.getClass()) return false; StringElement temp = (StringElement)obj; return (message.equalsIgnoreCase(temp.message)); } public int compareTo(DataElement obj){ StringElement temp = (StringElement)obj; int compareResult = message.compareToIgnoreCase(temp.message); int setInt = 0; if(compareResult == 0){ setInt = 0; } else if(compareResult > 0){ //System.out.println(message + " is greater than " + temp.message + "."); for reference setInt = 1; } else if(compareResult < 0){ //System.out.println(temp.message + " is greater than " + message + "."); for reference setInt = -1; } return setInt; } public void makeCopy(DataElement copy){ StringElement temp; temp = (StringElement)copy; message = temp.message; } public DataElement getCopy(){ DataElement copy = new StringElement(message); return copy; } }
I've already ruled out that you can't compare the strings because you aren't comparing more than one string...I assume. Any tips would be greatly appreciated.