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(); }
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();
}
}
Step by step
Solved in 5 steps with 1 images