X389: ArrayQueueBadEnqueue1

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 EmptyQueueException();
        }
    }

}

Another developer has implemented the enqueue method below but they've made a mistake. Your job is to 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.