X705: ArrayQueueBadEnqueue2

For the question below, assume the following implementation of ArrayQueue with a fixed-size array and one unused slot


public class ArrayQueue<T> implements QueueInterface<T> {
    private T[] contents;
    private int frontIndex;

    private int backIndex;
    private static final int DEFAULT_CAPACITY = 50;

    public ArrayQueue() {
        this(DEFAULT_CAPACITY);
    }

    @SuppressWarnings("unchecked")
    public ArrayQueue(int initialCapacity) {
        // The new array contains null entries
        contents = (T[])new Object[initialCapacity + 1];
        frontIndex = 0;
        backIndex = contents.length - 1;
    }


    @Override
    public void enqueue(T newEntry) {
        // TODO Auto-generated method stub

    }

    @Override
    public T dequeue() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public T getFront() {
        if (isEmpty()) {
            throw new EmptyQueueException();
        }

        return contents[frontIndex];
    }


    @Override
    public boolean isEmpty() {

        return (((backIndex + 1) % contents.length) == frontIndex);
    }


    @Override
    public void clear() {
        while (!isEmpty()) {
            dequeue();
        }

    }

    public void checkCapacity() {
        if (backIndex+2 % contents.length == frontIndex) {
            throw new FullQueueException();
        }
    }


}

After correcting their mistake you spoke to your fellow developer and explained how array based queues work. They have tried to implement it again but have still made a mistake. Again, correct that mistake and fix the broken code below.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.