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: 650362 • 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

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 rear = null;

    }

    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[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

         newQueue[rear] = this.dequeue();

         newIndex++;

        

}   

         //Set queue to reference the new array and update the front and rear pointers.

         queue = newQueue;

         front = rear;

         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

q.removeAt(1);

for (int i = 0; i < q.count; i++)

    {

   Console.WriteLine(" - " + q[i]);

    }

}    

}      

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