Question: import java.util.NoSuchElementException; public class CircularArrayQueue implements QueueADT { private static final int DEFAULT_SIZE = 5; // Example for declaring magic numbers private static final int

import java.util.NoSuchElementException; public class CircularArrayQueue implements QueueADT { private static final int DEFAULT_SIZE = 5; // Example for declaring magic numbers  private static final int GROWTH_FACTOR = 2; // Example for declaring magic numbers   private int[] circularArray; private int length; private int head; private int tail; public CircularArrayQueue() { # The default constructor which creates a new queue with a default initial capacity of 5. You can call other constructors by using the method this( ). // TODO  } public CircularArrayQueue(int capacity) { # The second constructor which creates a new queue with the specified capacity. If the capacity specified is less than 1, set the queues capacity to 1. // TODO  } public void add(int elem) { # Adds a new element to the back of the queue. 1) The queue should be able to accomodate for any number of elements. That is, regardless of your queues initial capacity, a call to add(int elem) will always successfully add an element to your queue. Thus, once the array that backs your queue becomes full, you need to resize it. This should be implemented in a private helper method resize(). 2) To simplify the implementation and avoid having a large chunk of code checking different conditions, you should use % operator here. It will return the remainder of the division x % y = r of x/y. For example, 5 % 3 = 2 because 5 divided by 3 results in a remainder of 2. Think about how you can use it to update the tail pointer. // TODO  } private void resize() { # Doubles the capacity of the queue. When expanding the capacity of your Queue, you will need to do the following: 1) Allocate a new, larger array 2) Copy the elements of your old array to this new array 3) Change the references. Make sure that your queues circular array is now bounded to this new one with a larger capacity. // TODO  } public boolean isEmpty() { # Determines if the queue is empty. Returns true if the queue is empty and false otherwise. // TODO   return false; } public int peek() throws NoSuchElementException { # Returns the front element of the queue. Throws NoSuchElementException if the queue is empty. // TODO   return 0; } public int remove() throws NoSuchElementException { # Returns and removes the front element of the queue. Throws NoSuchElementException if the queue is empty. Note: You should also use % operator here to update the head pointer. // TODO   return 0; } public void clear() { # Removes all elements from the queue. // TODO  } public int size() { # Returns the number of items in the queue. // TODO   return 0; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!