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: 3861373 • 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;

Explanation / Answer

myqueue.java

public class myqueue
{
public String[] array ;
int front,rear,size;

public myqueue(int n) {
    this.array = new String[n];
    this.front = -1;
    this.rear = -1;
    this.size = n;
}
public myqueue()
{
    this.array = new String[5];
    this.front = -1;
    this.rear = -1;
    this.size = 5;
}

public void enqueue(String value)
   {
       if((this.front == 0 && this.rear == this.size - 1) || (this.front == this.rear+1))
       {
                System.out.println("Queue is Full");
       }
       else
       {
          if(this.rear == this.size -1 && this.front != 0)
          {
           this.rear = -1;
          }
          this.rear = this.rear + 1;
          this.array[this.rear] = value;
          System.out.println("Inserted!");
          if(this.front == -1)
          {
           this.front = 0;
          }

       }
   }

public void dequeue()
   {
       if(this.front == -1 && this.rear == -1)
              {
                System.out.println("Queue is empty");
              }
       else{         
              System.out.println("Deleted element " + this.array[this.front]);
              this.front = this.front + 1;

              if(this.front-1 == this.rear)
              {
               this.front = -1;
               this.rear = -1;
              }

              if(this.front == this.size)
              {
                  this.front = 0;
              }

          }
   }

public boolean isfull()
   {
       if((this.front == 0 && this.rear == this.size - 1) || (this.front == this.rear+1))
       {
                System.out.println("Queue is Full");
                return true;
       }
       else
       {
           return false;
       }
   }

public boolean isempty()
   {

       if(this.front == -1 && this.rear == -1)
              {
                System.out.println("Queue is empty");
                return true;
              }
              else
              {
                  return false;
              }
   }

}

driver.java

public class driver
{
    public static void main(String[] args)
    {
     myqueue Q = new myqueue(5);
     Q.enqueue("abc1");
     Q.enqueue("abc2");
     Q.enqueue("abc3");
     Q.enqueue("abc4");
     Q.enqueue("abc5");
     Q.isfull();
     Q.enqueue("abc6");

     Q.dequeue();
     Q.dequeue();
     Q.dequeue();
     Q.dequeue();
     Q.dequeue();   

     Q.isempty();
     Q.dequeue();   
}
}

Sample Output:

Inserted!
Inserted!
Inserted!
Inserted!
Inserted!
Queue is Full
Queue is Full
Deleted element abc1
Deleted element abc2
Deleted element abc3
Deleted element abc4
Deleted element abc5
Queue is empty
Queue is empty

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