import java.util.Vector; import java.util.Iterator; import java.io.StringWriter; class MyStack { @invariant{ 0 <= size() && size() <= maxSize } protected Vector elements; protected int maxSize; protected int size; MyStack(int n){ elements = new Vector(n); maxSize = n; size = 0; } public int size() { return size; } public void push(int i){ @pre{ !full() } @post{ !empty() && top() == i && size() == old.size() + 1} Integer val = new Integer(i); elements.add(val); size=elements.size(); } public int pop(){ @pre{!empty()} @post{!full() && size() == old.size() - 1} Integer result = (Integer) elements.lastElement(); int index = elements.lastIndexOf(result); elements.remove(index); size = elements.size(); return result.intValue(); } public int top(){ Integer result = (Integer) elements.lastElement(); return result.intValue(); } public boolean full(){ return (size() == this.maxSize); } public boolean empty(){ return elements.isEmpty(); } public String printMe(){ StringWriter sw = new StringWriter(); int index = 0; Iterator it = elements.iterator(); while(it.hasNext()){ sw.write(" "+((Integer)it.next()).intValue()+" "); index++; } return sw.toString(); } }