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

Please do this in Java. Today you will create a Queue using an array size 12 It

ID: 3802710 • Letter: P

Question

   Please do this in Java.

Today you will create a Queue using an array size 12 It will be similar to the Stack code Iwent over in class yesterday. Use an array of objects to hold the data, nothing else, just like I did for the stack code. Here is the main you should use public static void main (String argsD Array Queue queue new Array Queue() for(int i 0: i 8; i++) queue enqueue (i) System out println Peaking queue peek 0.toStringO) System.out.println( Removing queue.dequeue0.toString0); System.out.println("******Removing t queue.dequeue O.toString0); forint 10, i 20, i++) queue enqueue (i), while(!queue.empty0) System.out.println("* Removing t queue.dequeue0.toString0), System.out.println("Final value for the front index queue.returnFront0), Here is my sample output with the main above........ Added to empty Queue 0 enqueued 1 enqueued 2 enqueued 3 enqueued 4 enqueued 5 enqueued 6 enqueued 7 @@@@@@Peaking 0 Removing 0 Removing 1 enqueued 10 enqueued 11 enqueued 12 enqueued 13 enqueued 14 enqueued 15 Sorry, queue is full Sorry, queue is full Sorry, queue is full Sorry, queue is full Removing 2 Removing 3 Removing 4

Explanation / Answer

Hi please find the below code with attached output

ArrayQueue.java

import java.util.NoSuchElementException;

public class ArrayQueue {
   int queueSize=12;
   int queue[] = new int[queueSize];
   int size=0,front=-1,rear=-1;
   //it will enqueue the element
   public void enqueue(int n){
       if(isEmpty()){
           if(rear==-1){
               rear=0;
               front=0;
               }
           queue[rear]=n;
           size++;
           System.out.println("Added to empty queue "+n);
           return;
       }
       if(rear+1>=queueSize){
           System.out.println("sorry ,queue is full");
       }
       else if(rear+1<queueSize){
           queue[++rear]=n;
           size++;
           System.out.println("enqueued "+n);
       }
      
   }
   //it will return top element
   public int peak(){
       if(isEmpty()){
           System.out.println("Empty Queue");
           return -1;
       }
       else{
           return queue[front];
       }
   }
   public int returnFront(){
       return front;
   }
   //it will remove front element
   public int dequeue(){
      
       if(isEmpty()){
           System.out.println("No element to remove, under flow");
           throw new NoSuchElementException("Under flow");
       }
      
       else{
           int removed=queue[front];
           size--;
           if(front==rear){
               front=-1;
               rear=-1;
           }
           else
           front++;
       return removed;
       }
      
   }
   //it will return queue is empty or not
   public boolean isEmpty(){
       return size==0;
   }
  
   //Main method for testing functionality
   public static void main(String[] args) {
       ArrayQueue queue = new ArrayQueue();
       for(int i=0;i<8;i++){
           queue.enqueue(i);
       }
       System.out.println("@@@@@@@@peaking "+queue.peak());
       System.out.println("*********Removing "+queue.dequeue());
       System.out.println("*********Removing "+queue.dequeue());
      
       for(int i=10;i<23;i++){
           queue.enqueue(i);
       }
       while(!queue.isEmpty()){
           System.out.println("************removing "+queue.dequeue());
       }
       System.out.println("final value of front index "+queue.returnFront());
   }

}

OUTPUT:

Added to empty queue 0
enqueued 1
enqueued 2
enqueued 3
enqueued 4
enqueued 5
enqueued 6
enqueued 7
@@@@@@@@peaking 0
*********Removing 0
*********Removing 1
enqueued 10
enqueued 11
enqueued 12
enqueued 13
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
sorry ,queue is full
************removing 2
************removing 3
************removing 4
************removing 5
************removing 6
************removing 7
************removing 10
************removing 11
************removing 12
************removing 13
final value of front index -1