How to implement Exception for function isFull() on Stack

2.2k views Asked by At
import java.util.*;
import java.lang.Iterable; 

public class MyStackArray <Item> implements Iterable<Item> {
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> 0 && 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 <= top; i++)
            t[i] = I[i];
        I = t;
}

public Iterator<Item> iterator() {
        return new MyStackArrayIterator();
}

private class MyStackArrayIterator implements Iterator <Item> {
        private int i = top;

        public boolean hasNext() {
            return (i > -1);
        } 

        public Item next() {
            return I[i--];
        }

        public void remove() {
           throw new UnsupportedOperationException();
        }
    }
}

That is the code for Stack using generic method. For isEmpty, everything went smoothly, that exception is work.

public boolean isFull() {
            return (top == I.length - 1);
}

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.

push("A");push("B");push("C");push("D");push("E");

size() = 5, getTop() = E

And then I push one more elements, my exception says that the Stack is full.

size() = 5, getTop() = E

So, I popped all of them.

Size = 0, getTop = null

I push 3 elements,

push("F");push("G");push("H");

But the program says that the stack is already full, while the maximum is 5 elements. How can I fix that?

1

There are 1 answers

0
Eran On

Your pop method halves the capacity of the Stack when you pop most of the elements (when top== I.length / 4).

Your push method is supposed to increase the capacity when necessary, but isFull() prevents it from doing so (since the same condition that isFull() checks - (top == I.length - 1) - is also used do determine when the capacity should be increased).

What does isFull() mean if you support increasing the capacity? Either the capacity is fixed, in which case you should never change it, or it's not fixed, in which case isFull() should always return false.