n this assignment, you should provide a complete CircularQueueDriver class that fully tests the functionality of your CircularQueue class that you have improved in Lab 8. Your CircularQueueDriver class should: • Instantiate a 3-element CircularQueue. • Use a loop to add strings to the queue until the add method returns false (which indicates a full queue). • Call showQueue. • Use a loop to remove strings from the queue until the remove method returns null (which indicates an empty queue). As you remove each string from the queue, print the removed string. Sample Run: Monsieur A Monsieur B Monsieur C removed: Monsieur A removed: Monsieur B removed: Monsieur C CircularQueue Class: public class CircularQueue {     private String[] queue; // array that implements a circular queue     private int length = 0; // number of filled elements     private int capacity; // size of array     private int front = 0; // index of first item in queue     private int rear = 0; // one past the index of the last item     // Instantiate the queue's array with the given capacity.     public CircularQueue(int capacity) {         queue = new String[capacity];         this.capacity = capacity;     }     public boolean isEmpty() {         return length == 0;     }     public boolean isFull() {         return length == capacity;     }     public int length() {         return length;     }     // Add a value to the rear of the queue by using the rear's     // current position and then incrementing rear.     public boolean add(String name) {         if (isFull()) {             return false;         }         queue[rear] = name;         rear = (rear + 1) % capacity; // if rear gets too big, assign it to 0         length++;         return true;     }     // Remove the value at the front of the queue and then increment     // front.     public String remove() {         if (isEmpty()) {             return null;         }         String result = queue[front];         queue[front] = null;         front = (front + 1) % capacity;         length--;         return result;     }     // Display the queue's contents in front-to-rear order.     public void showQueue() {         if (isEmpty()) {             return;         }         for (int i = front; i != rear; i = (i + 1) % capacity) {             System.out.println(queue[i]);         }     }          public static void main(String[] args) {         CircularQueue queue = new CircularQueue(5);         queue.add("Alice");         queue.add("Bob");         queue.add("Charlie");         queue.showQueue();         queue.remove();         queue.showQueue();     }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

In Java

In this assignment, you should provide a complete CircularQueueDriver class that fully
tests the functionality of your CircularQueue class that you have improved in Lab 8. Your
CircularQueueDriver class should:
• Instantiate a 3-element CircularQueue.
• Use a loop to add strings to the queue until the add method returns false (which
indicates a full queue).
• Call showQueue.
• Use a loop to remove strings from the queue until the remove method returns null
(which indicates an empty queue). As you remove each string from the queue, print the
removed string.
Sample Run:
Monsieur A
Monsieur B
Monsieur C
removed: Monsieur A
removed: Monsieur B
removed: Monsieur C

CircularQueue Class:

public class CircularQueue {
    private String[] queue; // array that implements a circular queue
    private int length = 0; // number of filled elements
    private int capacity; // size of array
    private int front = 0; // index of first item in queue
    private int rear = 0; // one past the index of the last item

    // Instantiate the queue's array with the given capacity.
    public CircularQueue(int capacity) {
        queue = new String[capacity];
        this.capacity = capacity;
    }

    public boolean isEmpty() {
        return length == 0;
    }

    public boolean isFull() {
        return length == capacity;
    }

    public int length() {
        return length;
    }

    // Add a value to the rear of the queue by using the rear's
    // current position and then incrementing rear.
    public boolean add(String name) {
        if (isFull()) {
            return false;
        }
        queue[rear] = name;
        rear = (rear + 1) % capacity; // if rear gets too big, assign it to 0
        length++;
        return true;
    }

    // Remove the value at the front of the queue and then increment
    // front.
    public String remove() {
        if (isEmpty()) {
            return null;
        }
        String result = queue[front];
        queue[front] = null;
        front = (front + 1) % capacity;
        length--;
        return result;
    }

    // Display the queue's contents in front-to-rear order.
    public void showQueue() {
        if (isEmpty()) {
            return;
        }
        for (int i = front; i != rear; i = (i + 1) % capacity) {
            System.out.println(queue[i]);
        }
    }
    
    public static void main(String[] args) {
        CircularQueue queue = new CircularQueue(5);
        queue.add("Alice");
        queue.add("Bob");
        queue.add("Charlie");
        queue.showQueue();
        queue.remove();
        queue.showQueue();
    }
}

Expert Solution
steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning