Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fill in the following blanks for JAVA code::: import java.util.NoSuchElementExce

ID: 650470 • Letter: F

Question

Fill in the following blanks for JAVA code:::

import java.util.NoSuchElementException;


public class CircularQueue<E>
{
   private E[] queue;
   private int front = 0, rear = 0;
   private static final int DEFAULT_CAPACITY = 5;
  

   public CircularQueue(int capacity)
   {
   queue = (E[]) new Object[capacity + 1];
   }
  
   public CircularQueue()
   {
       this(DEFAULT_CAPACITY);
}

  
   //Add a method that will determine if the queue is empty. Recall that the queue is empty if the front and
   //rear index the same location in the array:

   public boolean isEmpty()
   {
       return __________ = __________;
}

   private boolean isFull()
   {
       return ((rear + 1) % queue.length) == front;
}
  
   public void enqueue(E newElement)
   {
      
       if (isFull())
       {
      
       this.expand();
      
   }
       //To add an element to the rear of the queue, move the rear index up and add the element to the rear
       //location in the queue:
       rear = (rear + 1) % queue.length;
       queue[__________] = newElement;

}
  
   public E dequeue()
   {
       if(isEmpty())
       {
           throw new NoSuchElementException("Queue is empty");
       }  
          
           front = (front + 1) % queue.length;
           E frontElement = queue[front];  
           queue[front] = null;
           return frontElement;

   }  
          
   private void expand()
   {
  
       E[] newQueue = (E[]) new Object[(queue.length * 3) / 2 + 1];
  
       int newIndex = 1;
       while (!this.isEmpty())
       {
           // Dequeue all of the current elements
           //add them to the new array beginning at index 1
       newQueue[__________] = this.dequeue();
       newIndex++;
         
}  
       //Set queue to reference the new array and update the front and rear pointers.
       queue = newQueue;
       front = _____;
       rear = newIndex - 1;
   }
  
   }
public class QueueTest
{
   public static void main(String[] args)
{
CircularQueue<String> q = new CircularQueue<String>(4);
q.enqueue("Red");
q.enqueue("Yellow");
q.enqueue("Green");
q.enqueue("Blue");
q.enqueue("Purple");
q.enqueue("Orange");
q.enqueue("White");
System.out.println(q.dequeue());
System.out.println(q.dequeue());
q.enqueue("Grey");

///Create a for loop that removes and prints all elements from the queue
while(_____ )
{

}
}
}

Explanation / Answer

//Blanks are filled with bold letters and explainded in bold letters

import java.util.NoSuchElementException;
public class CircularQueue<E>
{
   private E[] queue;
   private int front = 0, rear = 0;
   private static final int DEFAULT_CAPACITY = 5;

   public CircularQueue(int capacity)
   {
       queue = (E[]) new Object[capacity + 1];
   }

   public CircularQueue()
   {
       this(DEFAULT_CAPACITY);
   }


   //Add a method that will determine if the queue is empty.
   //Recall that the queue is empty if the front and
   //rear index the same location in the array:

   public boolean isEmpty()
   {
       //The condtion front == rear that returns true if the front
       //and reart becomes equal then it becomes empty

       return front ==rear;
   }

   private boolean isFull()
   {
       return ((rear + 1) % queue.length) == front;
   }

   public void enqueue(E newElement)
   {

       if (isFull())
       {

           this.expand();

       }
       //To add an element to the rear of the queue, move the rear index up and add the element to the rear
       //location in the queue:
       rear = (rear + 1) % queue.length;
       //set index postion reart in queue to insert a new generic element newElement
       //to the queue.
       queue[rear] = newElement;

   }

   public E dequeue()
   {
       if(isEmpty())
       {
           throw new NoSuchElementException("Queue is empty");
       }

       front = (front + 1) % queue.length;
       E frontElement = queue[front];
       queue[front] = null;
       return frontElement;

   }

   private void expand()
   {
       E[] newQueue = (E[]) new Object[(queue.length * 3) / 2 + 1];

       int newIndex = 1;
       while (!this.isEmpty())
       {
           // Dequeue all of the current elements
           //add them to the new array beginning at index 1
           //Set newIndex to the newQueue to start inserting elements from old queue
           //to new expanded array newQueue
           newQueue[newIndex] = this.dequeue();

           newIndex++;

       }
       //Set queue to reference the new array and update the front and rear pointers.
       queue = newQueue;
       //Set reart position to front because the reart becomes the front
       front = rear;
       //rear position is the index which is newIndex less than 1.
       rear = newIndex - 1;

   }

}

--------------------------------------------------------------------------------------------------------------------------------

public class QueueTest
{
   public static void main(String[] args)
   {
       CircularQueue<String> q = new CircularQueue<String>(4);
       q.enqueue("Red");
       q.enqueue("Yellow");
       q.enqueue("Green");
       q.enqueue("Blue");
       q.enqueue("Purple");
       q.enqueue("Orange");
       q.enqueue("White");
       System.out.println(q.dequeue());
       System.out.println(q.dequeue());
       q.enqueue("Grey");

       ///Create a for loop that removes and prints all elements from the queue
       //Check that circular queue,q is isEmpty method

       while(q.isEmpty())
       {
           //call the method q.dequeue method to remove element
           //from circular queue,q from front end
           System.out.println(q.dequeue());
       }

   }
}

Hope this helps you..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote