/** * This is a simple stack implementation, using an array * of fixed size. Only a stack of int is considered. * It has been modified to throw exceptions and to implement the * Stack interface. */ /** * @author Melvin Fitting * @version March 5, 2008 * */ public class ArrayStack implements Stack { private static final int MAX_SIZE = 100; private int[] numbers = new int[MAX_SIZE]; private int count = 0; public boolean isEmpty(){ return count == 0; } public int peek(){ if (count==0) throw new StackException("Stack underflow in peek"); return numbers[count-1]; } public void push(int n){ if (count==numbers.length) throw new StackException("Stack overflow in push"); numbers[count] = n; count++; //numbers[count++] = n; } public int pop(){ if (count==0) throw new StackException("Stack underflow in pop"); count--; return numbers[count]; //return numbers[--count]; } }