Class Name: 
kiasan.examples.stack.StackAr

Report Rendered: Mon May 04 11:28:33 CDT 2009, by Sireum/Kiasan for Java v0.1.20090504

Methods Covered:
PercentRatio
Class / Method T E Instruction Coverage Branch Coverage Time
1 1
  31/33
  93.94%
  3/4
  75%
0.195s
1 2
  37/37
  100%
  4/4
  100%
0.203s
1 1
  77/87
  88.51%
  9/14
  64.29%
0.235s
Total
3 4
  145/157
  92.36%
  16/22
  72.73%
0.633s
Source Code:
1   package kiasan.examples.stack;
2   
3   import kiasan.examples.common.Overflow;
4   import kiasan.examples.common.Underflow;
5   
6   // StackAr class
7   //
8   // CONSTRUCTION: with or without a capacity; default is 10
9   //
10  // ******************PUBLIC OPERATIONS*********************
11  // void push( x )         --> Insert x
12  // void pop( )            --> Remove most recently inserted item
13  // Object top( )          --> Return most recently inserted item
14  // Object topAndPop( )    --> Return and remove most recently inserted item
15  // boolean isEmpty( )     --> Return true if empty; else false
16  // boolean isFull( )      --> Return true if full; else false
17  // void makeEmpty( )      --> Remove all items
18  // ******************ERRORS********************************
19  // Overflow and Underflow thrown as needed
20  
21  /**
22   * Array-based implementation of the stack.
23   * 
24   * @author Mark Allen Weiss
25   */
26  public class StackAr {
27    private final Object[] theArray;
28  
29    private int topOfStack;
30  
31    static final int DEFAULT_CAPACITY = 10;
32  
33    public static void main(final String[] args) {
34      final StackAr s = new StackAr(12);
35  
36      try {
37        for (int i = 0; i < 10; i++) {
38          s.push(new Integer(i));
39        }
40      } catch (final Overflow e) {
41        System.out.println("Unexpected overflow");
42      }
43  
44      while (!s.isEmpty()) {
45        System.out.println(s.topAndPop());
46      }
47    }
48  
49    /**
50     * Construct the stack.
51     */
52    public StackAr() {
53      this(StackAr.DEFAULT_CAPACITY);
54    }
55  
56    /**
57     * Construct the stack.
58     * 
59     * @param capacity
60     *          the capacity.
61     */
62    public StackAr(final int capacity) {
63      this.theArray = new Object[capacity];
64      this.topOfStack = -1;
65    }
66  
67    /**
68     * Test if the stack is logically empty.
69     * 
70     * @return true if empty, false otherwise.
71     */
72    public boolean isEmpty() {
73      return this.topOfStack == -1;
74    }
75  
76    /**
77     * Test if the stack is logically full.
78     * 
79     * @return true if full, false otherwise.
80     */
81    public boolean isFull() {
82      return this.topOfStack == this.theArray.length - 1;
83    }
84  
85    /**
86     * Make the stack logically empty.
87     */
88    public void makeEmpty() {
89      this.topOfStack = -1;
90    }
91  
92    /**
93     * Remove the most recently inserted item from the stack.
94     * 
95     * @exception Underflow
96     *              if stack is already empty.
97     */
98    //@ requires (this.theArray != null) && (this.theArray.length >= 0) && (this.topOfStack >= -1) && (this.topOfStack < this.theArray.length);
99    //@ ensures true;
100   public void pop() throws Underflow {
101     if (isEmpty()) {
102       throw new Underflow();
103     }
104     this.theArray[this.topOfStack--] = null;
105   }
106 
107   /**
108    * Insert a new item into the stack, if not already full.
109    * 
110    * @param x
111    *          the item to insert.
112    * @exception Overflow
113    *              if stack is already full.
114    */
115   //@ requires (this.theArray != null) && (this.theArray.length > 0) && (this.topOfStack >= -1) && (this.topOfStack < this.theArray.length);
116   //@ ensures this.theArray[this.topOfStack] == x;
117   public void push(final Object x) throws Overflow {
118     if (isFull()) {
119       throw new Overflow();
120     }
121     this.theArray[++this.topOfStack] = x;
122   }
123 
124   //@ requires (this.theArray != null) && (this.theArray.length > 0) && !isFull() && (this.topOfStack >= -1) && (this.topOfStack < this.theArray.length);
125   //@ ensures true;
126   public void pushPop(final Object x) throws Overflow {
127     push(x);
128     final Object y = topAndPop();
129     assert x == y;
130   }
131 
132   /**
133    * Get the most recently inserted item in the stack. Does not alter the stack.
134    * 
135    * @return the most recently inserted item in the stack, or null, if empty.
136    */
137   public Object top() {
138     if (isEmpty()) {
139       return null;
140     }
141     return this.theArray[this.topOfStack];
142   }
143 
144   /**
145    * Return and remove most recently inserted item from the stack.
146    * 
147    * @return most recently inserted item, or null, if stack is empty.
148    */
149   public Object topAndPop() {
150     if (isEmpty()) {
151       return null;
152     }
153     final Object topItem = top();
154     this.theArray[this.topOfStack--] = null;
155     return topItem;
156   }
157 
158 }