public class ArrayQueue2 // array implementation of queue (named ArrayQueue2 because I already have an ArrayQueue implemented) { private int[] queue; // queue is an array of ints (no Generics) private int front, rear, size; // front points to the index of the current front of the queue, rear at the current rear of the queue, size is the number of elements // we will use size to illustrate the problem with an array for a queue public ArrayQueue2() // constructor 1: set size to 100, initialize front, rear and size as expected { queue=new int[100]; front=0; rear=-1; size=0; } public ArrayQueue2(int size) //constructor 2: set size to the value passed in from user class, set front, rear, size as expected { queue=new int[size]; front=0; rear=-1; size=0; } public int peek() throws QueueEmptyException // return the element at front of queue as long as rear!=-1, which would imply nothing has been enqueued yet { if(rear==-1) throw new QueueEmptyException("Queue is empty, cannot peek"); else return queue[front]; } public int dequeue() throws QueueEmptyException // remove item at front, increment front, decrement size { if(rear==-1) throw new QueueEmptyException("Queue is empty, cannot dequeue"); else { int temp=queue[front]; front++; size--; return temp; } } public boolean isEmpty() // queue is empty if size==0 { return size==0; } public boolean isFull() // queue should be full when size==queue.length, but we are using rear to indicate full, this will be explained in class { return rear==queue.length-1; } public void enqueue(int x) throws QueueFullException // current rear of queue is at position rear, to enqueue, add 1 to rear first, add new element there, increment size { if(rear==queue.length-1) throw new QueueFullException("Cannot enqueue " + x + " onto full queue"); else { rear++; queue[rear]=x; size++; } } public int getSize() // this accessor is usually not needed, but illustrates the problem with the array queue implementation { // this value may not be equal to queue.length and yet the Queue thinks its full! return size; } }