public class ArrayQueue implements Queue { private int capacity; private Object[] queue; private int ini = 0; private int prox = 0; public ArrayQueue(int capacity) { this.capacity = capacity; queue = new Object[capacity + 1]; } @Override public void enqueue(T obj) throws QueueFullException { if (isFull()) { throw new QueueFullException(); } queue[prox] = obj; prox = (prox + 1) % queue.length; } @Override public T dequeue() throws QueueEmptyException { if (isEmpty()) { throw new QueueEmptyException(); } Object dequeued_obj = queue[ini]; queue[ini] = null; ini = (ini + 1) % queue.length; return (T) dequeued_obj; } @Override public int size() { if (prox >= ini) { return prox - ini; } else { return queue.length - (ini - prox); } } @Override public boolean isEmpty() { return size() == 0; } @Override public boolean isFull() { return size() == capacity; } @Override public T front() throws QueueEmptyException { if (isEmpty()) { throw new QueueEmptyException(); } return (T) queue[ini]; } @Override public void empty() { while (!isEmpty()) { try { dequeue(); } catch (QueueEmptyException e) { e.printStackTrace(); } } } }