Java Q&A
Krishnan Rangaraajan


Listing One
class MyStack {
   private Object[] elems; 
   private int top, max; 
   public MyStack(int sz) {
      max = sz;
      elems = new Object[sz];
   }
   public void push(Object obj) throws Exception {
      if( top < max )
         elems[top++] = obj;
      else throw new Exception("Stack overflow");
   }
   public Object pop() throws Exception {
      if( top > 0 )
         return elems[--top];
      throw new Exception("Stack underflow");
   }
   public boolean isFull() {
      return top == max;
   }
   public boolean isEmpty() {
      return top == 0;
   }
}

Listing Two
class StackTester {
   public static void main(String[] args) {
      MyStack s1 = new MyStack (10); // Max of 10 elements
      if( !s1.isEmpty() )
        Test.error("Stack is not empty initially!");
      StackTester obj1 = new StackTester(); // An object to stack
      s1.push(obj1);
      if( s1.isEmpty() )
        Test.error("Stack is empty after a push!");
      StackTester obj2 = (StackTester) s1.pop();
      if( obj1 != obj2 )
        Test.error("Problem in push()/pop()!"); // ----- (A)
           // ... Other code
      }
}

Listing Three
class MyStack {
    // Private elements are public - FOR TESTING ONLY
    public Object[] elems; 
    // Private elements are public - FOR TESTING ONLY
    public int top, max; 
    public MyStack(int sz) {
       max = sz;
       elems = new Object[sz];
    }
    public void push(Object obj) throws Exception {
       if( top < max )
          elems[top++] = obj;
    else throw new Exception("Stack overflow");
    }
       public Object pop() throws Exception {
       if( top > 0 )
          return elems[--top];
       throw new Exception("Stack underflow");
    }
    public boolean isFull() {
       return top == max;
    }
    public boolean isEmpty() {
       return top == 0;
    }
}

Listing Four
class StackTester {
  public static void main(String[] args) {
    MyStack s1 = new MyStack (10); // Max of 10 elements
    if( !s1.isEmpty() ) 
    Test.error( "Stack is not empty initially!");
    StackTester obj1 = new StackTester(); // An object to stack
    s1.push(obj1);
    Test.assert((s1.top == 1) && (s1.elems[s1.top-1] == obj1),"Push failed!");
    StackTester obj2 = (StackTester) s1.pop();
    Test.assert( (s1.top == 0) && (obj2 == obj1), "Pop failed!");
    // ... Other code
    }
}

Listing Five
/** @inv (top >= 0) && (top < max) */
class MyStack {
   private Object[] elems; 
   private int top, max; 
   /** @post (max == sz) && (top == 0) */
   public MyStack(int sz) {
      max = sz;
      elems = new Object[sz];
   }
   /** @pre top < max; 
   *   @post (top == top$prev + 1) && (elems[top-1] == obj)
   */
   public void push(Object obj) throws Exception {
     if( top < max )
        elems[top++] = obj;
      else throw new Exception("Stack overflow");
   }
   /**  <assertions here> */
   public Object pop() throws Exception {
      if( top > 0 )
         return elems[--top];
      throw new Exception("Stack underflow");
   }
   public boolean isFull() {
      return top == max;
   }
   public boolean isEmpty() {
      return top == 0;
  }
}

Listing Six
class MyStack {
   // ... Insert stack related elements here.
   // ...
   // This is the stack test driver. It is part of the class itself.
   public static void main(String[] args) {
      MyStack s1 = new MyStack (10); // Max of 10 elements
      if( !s1.isEmpty() ) 
         Test.error( "Stack is not empty initially!");
      MyStack obj1 = new MyStack (); // An object to stack 
      s1.push(obj1);
      Test.assert( (s1.top == 1) && (s1.elems[s1.top-1] == obj1), 
                                                           "Push failed!");
      MyStack obj2 = (MyStack) s1.pop();
      Test.assert( (s1.top == 0) && (obj2 == obj1), "Pop failed!");
      // ... Other code
   }
}

3


