Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I started this but I am getting some errors. Please help. Circular Array.java public class CircularArrayQueue implements QueueADT { private int front; private int rear;

image text in transcribedimage text in transcribed

I started this but I am getting some errors. Please help.

Circular Array.java

public class CircularArrayQueue implements QueueADT { private int front; private int rear; private int count; private T[] queue; private final int DEFAULT_CAPACITY=20; public CircularArrayQueue() { front=1; rear= DEFAULT_CAPACITY; count=0; queue=(T[])(new Object[DEFAULT_CAPACITY]); } public CircularArrayQueue(int initialCapacity) { front=1; rear= initialCapacity; count=0; queue=(T[])(new Object[initialCapacity]); } public void enqueue (T element){ if (size() == queue.length) expandCapacity(); queue[rear-1] = element; rear = (rear+1) % queue.length; count++; } public T dequeue() { if(isEmpty()) throw new EmptyCollectionException("The Queue is empty."); T element = queue[front]; queue[front]=null; count--; if(isEmpty()) rear = DEFAULT_CAPACITY; front = (front + 1)%queue.length; return element; } public T first() { if(isEmpty()) throw new EmptyCollectionException("Circular Queue"); return queue[front]; } public boolean isEmpty() { return (count == 0); } public int size() { return count; } public int getFront() { return front; } public int getRear() { return rear; } public int getLength() { return queue.length; } public String toString() { String result = "QUEUE: "; if(isEmpty()) { return "The queue is empty"; } for(int i=0;i

TesrQueue.java

public class TestQueue {

public static void main (String[] args) {

// --------------- Test 1 --------------- [isEmpty and toString]

CircularArrayQueue Q = new CircularArrayQueue();

boolean test1Success = false; if (Q.isEmpty() && Q.toString().equals("The queue is empty")) { test1Success = true; }

if (test1Success) { System.out.println("Test 1 passed"); } else { System.out.println("Test 1 failed"); }

// --------------- Test 2 --------------- [enqueue and size]

boolean test2Success = false; //System.out.println(queue); Q.enqueue("one"); Q.enqueue("two"); Q.enqueue("three"); Q.enqueue("four"); Q.enqueue("five"); System.out.println("Size is " + Q.size() + "and " + Q.toString()); if (Q.size() == 5 && Q.toString().equals("QUEUE: one, two, three, four, five.")) { test2Success = true; }

if (test2Success) { System.out.println("Test 2 passed"); } else { System.out.println("Test 2 failed"); }

// --------------- Test 3 --------------- [expandCapacity and size]

boolean test3Success = false; String[] letters = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

for (int i = 0; i

if (test3Success) { System.out.println("Test 3 passed"); } else { System.out.println("Test 3 failed"); }

// --------------- Test 4 --------------- [dequeue]

boolean test4Success = false; boolean t0 = false, t1 = false, t2 = false; String str = null; for (int i = 0; i

for (int i = 0; i

if (t0 && t1 && t2) { test4Success = true; }

if (test4Success) { System.out.println("Test 4 passed"); } else { System.out.println("Test 4 failed"); }

// --------------- Test 5 --------------- [first]

boolean test5Success = false; t0 = t1 = t2 = false; if (Q.first().equals("n")) { t0 = true; } for (int i = 0; i

if (Q.first().equals("c")) { t1 = true; } for (int i = 0; i

if (t0 && t1 && t2) { test5Success = true; }

if (test5Success) { System.out.println("Test 5 passed"); } else { System.out.println("Test 5 failed"); }

}

}

CircularArrayQueue.java . This class represents a Queue implementation using a circular array as the underlying data structure. This class must implement the QueueADT and work with the generic type (T). This class must have the following private variables: front (int) rear (int) count (int) queue (T array) DEFAULT_CAPACITY (final int) with a value of 20 The class must have the following public methods: CircularArrayQueue (constructor)- no parameters required in this first constructor. Initialize the front to 1, rear to the default capacity (DEFAULT_CAPACITY), count to 0, and the queue array using the final int variable DEFAULT_CAPACITY as the array's capacity. . CircularArrayQueue (second constructor) - same as the first constructor described above, except that this one takes in an int parameter for the initial capacity rather than using the default capacity. Front and rear are set to 1 and initialCapacity respectively. enqueue takes in an element of the generic type and adds that element to the rear of the queue. If the queue is full before adding this item, then call expandCapacity. dequeue throws an Empty CollectionException if the queue is empty; otherwise remove and return the item at the front of the stack. first - throws an Empty CollectionException if the queue is empty; otherwise return the item at the front of the queue without removing it. isEmpty - returns true if the queue is empty, and false otherwise. size - returns the number of items on the queue. getFront - returns the front index value (NOTE: this is not part of the QueueADT but is still required for this assignment). getRear - returns the rear index value (NOTE: this is not part of the QueueADT but is still required for this assignment). getLength - returns the current length (capacity) of the array (NOTE: this is not part of the QueueADT but is still required for this assignment). toString - returns the string containing "QUEUE: "followed by each of the queue's items in order from front to rear with ", " between each of the items and a period at the end of the last item. If the queue is empty then print "The queue is empty" instead. expandCapacity (private) - create a new array that has 20 more slots than the current array has, and transfer the contents into this new array and then point the queue instance variable to this new array by resetting the front and rear appropriately. You may set the front to 1 and the rear to count when expanding the array instead of the DEFAULT_CAPACITY based front and rear

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago