(Solved) : P Ublic Final Class Arrayqueue Implements Queueinterface Private T Queue Circular Array Qu Q41386921 . . .

For this lab, implement the ADT Queue by using a circular array to contain its entries. Expand the array dynamically as necesp

ublic final class ArrayQueue<T> implementsQueueInterface<T>

{

private T[] queue; // Circular array of queue entries and oneunused location

private int frontIndex;

private int backIndex;

private boolean initialized = false;

private int countOfEntries = 0;

private static final int DEFAULT_CAPACITY = 50;

private static final int MAX_CAPACITY = 10000;

public ArrayQueue()

{

this(DEFAULT_CAPACITY);

} // end default constructor

public ArrayQueue(int initialCapacity)

{

checkCapacity(initialCapacity);

// The cast is safe because the new array contains nullentries

@SuppressWarnings(“unchecked”)

T[] tempQueue = ( T[] ) new Object[initialCapacity];

queue = tempQueue;

frontIndex = 0;

backIndex = initialCapacity – 1;

initialized = true;

} // end constructor

private void checkInitialization()

{

// STUB

} // end checkInitialization()

private void checkCapacity(int initialCapacity)

{

// STUB

} // end checkCapacity()

// Doubles the size of the array queue if it is full

// Precondition: checkInitialization has been called.

// Modified

private void ensureCapacity()

{

if (countOfEntries == queue.length) // if array is full,

{ // double size of array

T[] oldQueue = queue;

int oldSize = oldQueue.length;

int newSize = 2 * oldSize;

checkCapacity(newSize);

// The cast is safe because the new array contains nullentries

@SuppressWarnings(“unchecked”)

T[] tempQueue = (T[]) new Object[2 * oldSize];

queue = tempQueue;

for (int index = 0; index < oldSize; index++)

{

queue[index] = oldQueue[frontIndex];

frontIndex = (frontIndex + 1) % oldSize;

} // end for

frontIndex = 0;

backIndex = oldSize – 1;

} // end if

} // end ensureCapacity()

@Override

public void enqueue(T newEntry)

{

checkInitialization();

ensureCapacity();

backIndex = (backIndex + 1) % queue.length;

queue[backIndex] = newEntry;

} // end enqueue()

@Override

public T dequeue()

{

checkInitialization();

if (isEmpty())

throw new EmptyQueueException();

else

{

T front = queue[frontIndex];

queue[frontIndex] = null;

frontIndex = (frontIndex + 1) % queue.length;

return front;

} // end if

} // end dequeue()

@Override

public T getFront()

{

// STUB

return null;

} // end getFront()

@Override

public boolean isEmpty()

{

// STUB

return false;

} // end isEmpty()

@Override

public void clear()

{

// STUB

} // end clear()

} // end class ArrayQueue

EmptyQueueException.java:

public class EmptyQueueException extends RuntimeException

{

private static final long serialVersionUID = 1L;

public EmptyQueueException()

{

this(null);

} // end default constructor

public EmptyQueueException(String message)

{

super(message);

} // end constructor

} // end class EmptyQueueException

QueueInterfece.java

public interface QueueInterface<T>
{
/** Adds a new entry to the back of this queue.
@param newEntry An object to be added. */
public void enqueue(T newEntry);
  
/** Removes and returns the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty before theoperation. */
public T dequeue();
  
/** Retrieves the entry at the front of this queue.
@return The object at the front of the queue.
@throws EmptyQueueException if the queue is empty. */
public T getFront();
  
/** Detects whether this queue is empty.
@return True if the queue is empty, or false otherwise. */
public boolean isEmpty();
  
/** Removes all entries from this queue. */
public void clear();
} // end QueueInterface

For this lab, implement the ADT Queue by using a circular array to contain its entries. Expand the array dynamically as necessary to make sure its capacity fits all the entries. Count entries to ascertain whether the queue is empty or full. Your implementation should differ from the book’s implementation in the following ways: • Your queue must use all array elements when the array is full – use the class variable countOf Entries to determine if the queue is full or empty. • You must implement clear() with O(1) efficiency – you cannot use a loop to dequeue/delete entries. You must implement the currently unimplemented methods in Array Queue.java. In addition, you should modify the bodies of the already-implemented methods to account for the new variable countOfEntries. When you modify how many entries are in the queue, change the variable’s value accordingly. Show transcribed image text For this lab, implement the ADT Queue by using a circular array to contain its entries. Expand the array dynamically as necessary to make sure its capacity fits all the entries. Count entries to ascertain whether the queue is empty or full. Your implementation should differ from the book’s implementation in the following ways: • Your queue must use all array elements when the array is full – use the class variable countOf Entries to determine if the queue is full or empty. • You must implement clear() with O(1) efficiency – you cannot use a loop to dequeue/delete entries. You must implement the currently unimplemented methods in Array Queue.java. In addition, you should modify the bodies of the already-implemented methods to account for the new variable countOfEntries. When you modify how many entries are in the queue, change the variable’s value accordingly.

Expert Answer


Answer to p ublic final class ArrayQueue implements QueueInterface { private T[] queue; // Circular array of queue entries and one…