X707: ArrayQueueManipulate2

For the question below, assume the following implementation of ArrayQueue:


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

    }

}

For this question you will be working with an array queue of integers that has already been instantiated and had some values enqueued:

ArrayQueue <Integer> arr = arr = new ArrayQueue <Integer>(7);
arr.enqueue(13);
arr.enqueue(12);
arr.enqueue(11);
arr.enqueue(10);
arr.enqueue(9);
arr.enqueue(8);
arr.enqueue(7);

Thus, the contents array currently looks like this:

[13, 12, 11, 10, 9, 8, 7, null]

Using only enqueue and dequeue, transform the contents array to look like this:

[5, 4, 3, null, 9, 8, 7, 6]

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.