title: How to implement Exception for function isFull() on Stack
body: <pre><code>import java.util.*;
  import java.lang.Iterable; 
  
  public class MyStackArray &lt;Item&gt; implements Iterable&lt;Item&gt; {
  private Item I[];
          private int top;
          private int size;
          private final static int DEFAULT_SIZE = 10;
  
  public MyStackArray () {
          this(DEFAULT_SIZE);
  }
  
  public MyStackArray (int capacity) {
          size = capacity;
          I = (Item[]) new Object [capacity];
          top = -1;
  }
  
  public Item getTop() {
          if (isEmpty())
              return null;
          return I[top];
  }
  
  public boolean isEmpty() {
          return (top == -1);
  }
  
  public boolean isFull() {
          return (top == I.length - 1);
  }
  
  public Item pop() throws EmptyStackException {
          if (isEmpty())
              throw new EmptyStackException ();
          Item item = I[top];
          I[top--] = null;
          if(top&gt; 0 &amp;&amp; top== I.length / 4)
              resize(I.length/2);
          return item;
  }
  
  public void push(Item item) throws FullStackException {
          if (isFull())
              throw new FullStackException ();
          if (top== I.length - 1)
              resize(2 * I.length);
          I[++top] = item;
  }
  
  public int size() {
          return (top+ 1);
  }
  
  private void resize (int newCapacity) {
          Item t[] = (Item[]) new Object[newCapacity];
          for (int i = 0; i &lt;= top; i++)
              t[i] = I[i];
          I = t;
  }
  
  public Iterator&lt;Item&gt; iterator() {
          return new MyStackArrayIterator();
  }
  
  private class MyStackArrayIterator implements Iterator &lt;Item&gt; {
          private int i = top;
  
          public boolean hasNext() {
              return (i &gt; -1);
          } 
  
          public Item next() {
              return I[i--];
          }
  
          public void remove() {
             throw new UnsupportedOperationException();
          }
      }
  }
  </code></pre>
  
  <p>That is the code for Stack using generic method.
  For isEmpty, everything went smoothly, that exception is work.</p>
  
  <pre><code>public boolean isFull() {
              return (top == I.length - 1);
  }
  </code></pre>
  
  <p>What should I change that isFull() and push() exception can work correctly?
  In driver class, I tried to push 5 elements when the max of elements is 5. </p>
  
  <pre><code>push("A");push("B");push("C");push("D");push("E");
  
  size() = 5, getTop() = E
  </code></pre>
  
  <p>And then I push one more elements, my exception says that the Stack is full.</p>
  
  <pre><code>size() = 5, getTop() = E
  </code></pre>
  
  <p>So, I popped all of them.</p>
  
  <pre><code>Size = 0, getTop = null
  </code></pre>
  
  <p>I push 3 elements, </p>
  
  <pre><code>push("F");push("G");push("H");
  </code></pre>
  
  <p>But the program says that the stack is already full, while the maximum is 5 elements. How can I fix that?</p>