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

Using the Queue ADT: Write a segment of code (application level) to perform each

ID: 3862598 • Letter: U

Question

Using the Queue ADT: Write a segment of code (application level) to perform each of the following operations. Assume myQueue is an object of the class ArrayBounded Queue and is a queue of strings, containing at least three elements. You may call any of the public methods. You may also use additional ArrayBoundedQueue objects. a. Set the string variable secondElement to the second element from the beginning of myQueue, leaving myQueue without its original two front elements. b. Set the string variable last equal to the rear element in myQueue, leaving myQueue empty. c. Set the string variable last equal to the rear element in myQueue, leaving myQueue unchanged. d. Print out the contents of myQueue, leaving myQueue unchanged.

Explanation / Answer

1. MyQueue.java

class MyQueue {

   // array is used to hold queue elements
   // two integer variables are used for front and rear pointers
   String items[] = new String[3];
   int front, rear;

   MyQueue() {
       front = 0;
       rear = -1;
   }

   //if queue is not full insert new element at rear end of queue
   void insert(String e) {
       if (rear == 2)
           System.out.println("Queue overflow");
       else {
           items[++rear] = e;
       }
   }

   // Return 1 if queue is empty and 0 otherwise
   int empty() {

       return (rear < front ? 1 : 0);
   }

   //if queue is not empty remove one element from front
   void remove() {
       if (empty() == 1)
           System.out.println("Queue Underflow");
       else
           System.out.println("Removed element : " + items[front++]);
      
   }

   //Display all elements from front to rear end of queue
   void display() {
       if (empty() == 0) {
           System.out.print("Queue : ");
           int t = front;
           while (t <= rear)
               System.out.print(" " + items[t++]);
           System.out.println();

       }
   }

}

2. Test.java

import java.util.Scanner;

class Test {
   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
       MyQueue q = new MyQueue();
       int ch = 0;
       String element;
       while (true) {
           System.out.println(" QUEUE");
           System.out.println("1.Insert");
           System.out.println("2.Remove");
           System.out.println("3.Exit");
           System.out.print("Enter your choice : ");
           try {
               ch = sc.nextInt();
           } catch (Exception e) {
               System.out.println("Please enter choice as number");
               return;
           }
           switch (ch) {
           case 1:
               System.out.print("Enter value : ");
               element = sc.next();
               q.insert(element);
               q.display();
               break;
           case 2:
               q.remove();
               q.display();
               break;
           case 3:
               return;
           }

       }

   }
}

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