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

Build and use your own minimal queue class using an array of type String of fixe

ID: 3806651 • Letter: B

Question

Build and use your own minimal queue class using an array of type String of fixed size. to hold the queue.

The array should have the default size of 5.

The array size should be setable via a constructor.

The following methods must be implemented:

enqueue – inserts a value at the rear of the queue

dequeue – returns the value at the front of the queue, removing the value from the queue

isEmpty – returns true if the queue is empty, false otherwise

isFull - – returns true if the queue is full, false otherwise

Tip: In a queue of 2 or more elements, the index of the rear is “larger” than the index of the front – EXCEPT, the queue will need to “wrap around” the array. For example: If the array is 20 elements. the queue front is at index 15 and the queue rear is at index 19, the next enqueue will be at index 20, which doesn't exist. The index must “wrap around” the array, i. e. the next enqueue will be at index 0. The solution is to modulus by the array size whenever increasing an index, either rear of front. For example, to change the index of the rear to the next index,use the formula

rear = (rear + 1) % arraySize;

Please notice that this must be done using an array not an arraylist or any other list, I think im close but am having trouble with a couple things, Thanks

Explanation / Answer

PROGRAM:

package queue;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Queue

{

   private double[] queue=new double[5];

private int size=5;

private int front=0;

private int rear=0;

public Queue()

{

      

}

//Parameterized constructor for initializing array size

public Queue(int size)

{

this.queue = new double[size];

this.front = 0;

this.rear=0;

this.size = size;

}

//inserting values in to queue

public void enQueue(double obj)

{

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

      

   if (rear == front)

   { //if full

   System.out.println("Queue is fully loaded.");

   if (rear == 0)

       rear = queue.length - 1;

   else

       rear = rear - 1;

   return;

   }

   queue[rear] = obj;

  

}

//checking whether queue is empty

public boolean isEmpty()

{

if(this.front==-1 && this.rear==-1)

{

   return true;

}

   return false;

}

// //checking whether queue is full

public boolean isFull()

{

   if(rear==0 && front==size-1)

   {

       return true;

   }

return false;

}

//entering the values from the queue

public double deQueue()

{

   if (rear == front) {//if empty

System.out.println("Queue is empty.");

return -1;

}

double tmp = queue[front+1];

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

return tmp;

}

//main function

public static void main(String s[]) throws NumberFormatException, IOException

{

   //variable type queue

   Queue q1= null;

   //InputStreamReader & BufferedReader for reading the input

   InputStreamReader in=new InputStreamReader(System.in);

   BufferedReader br=new BufferedReader(in);

   int choice=0;

   double number=0;

   System.out.println("1. user default size of array:");

       System.out.println("2. custum size");

       choice=Integer.parseInt(br.readLine().toString());

       //checking user is using default size of an array or customized size

       if(choice==1)

       {

           q1=new Queue();

       }

       else if(choice==2)

       {

           System.out.println(" enter size of array:");

           int size=Integer.parseInt(br.readLine());

           q1=new Queue(size);

       }

      

       loop:do

   {

       System.out.println("1. Enqueur");

       System.out.println("2. Dequeue");

       System.out.println("3. is empty");

       System.out.println("4. is full");

       System.out.println("5. exit");

          

       System.out.println("Enter the Choice:");

       choice=Integer.parseInt(br.readLine());

       switch(choice)

       {

       case 1:

           //pushing the value to the queue

           try

           {

           System.out.println("Enter the number:");

           number=Double.parseDouble(br.readLine());

           q1.enQueue(number);

           }

           catch(IndexOutOfBoundsException e)

           {

               System.out.println("Array is full");

           }

           break;

       case 2:

           //poping the value from the queue

           System.out.println(q1.deQueue());

           break;

       case 3:

           //checking the queue whether it is empty or not

           System.out.println(q1.isEmpty());

           break;

       case 4:

           //checking the queue whether it is full or not

           System.out.println(q1.isFull());

           break;

       case 5:

              

           break loop;

           default:

               System.out.println("Enter a valid choice");

       }

              

          

   }while(true);

      

      

}

}

OUTPUT

1. user default size of array:

2. custum size

1

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

1

Enter the number:

2

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

2

2.0

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

1

Enter the number:

1

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

1

Enter the number:

1

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

3

false

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

4

false

1. Enqueur

2. Dequeue

3. is empty

4. is full

5. exit

Enter the Choice:

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