1  public class StackOfIntegers {
  2    private int[] elements;
  3    private int size;
  4    public static final int DEFAULT_CAPACITY = 16;
  5  
  6    /** Construct a stack with the default capacity 16 */
  7    public StackOfIntegers() {
  8      this(DEFAULT_CAPACITY);
  9    }
 10  
 11    /** Construct a stack with the specified capacity */
 12    public StackOfIntegers(int capacity) {
 13      elements = new int[capacity];
 14    }
 15  
 16    /** Push a new integer into the top of the stack */
 17    public void push(int value) {
 18      if (size >= elements.length) {
 19        int[] temp = new int[elements.length * 2];
 20        System.arraycopy(elements, 0, temp, 0, elements.length);
 21        elements = temp;
 22      }
 23  
 24      elements[size++] = value;
 25    }
 26  
 27    /** Return and remove the top element from the stack */
 28    public int pop() {
 29      return elements[--size];
 30    }
 31  
 32    /** Return the top element from the stack */
 33    public int peek() {
 34      return elements[size - 1];
 35    }
 36  
 37    /** Test whether the stack is empty */
 38    public boolean isEmpty() {
 39      return size == 0;
 40    }
 41  
 42    /** Return the number of elements in the stack */
 43    public int getSize() {
 44      return size;
 45    }
 46  }