Question: Need help with this recursive java method. Using the operations for a Queue defined in class, write a recursive method that receives an instance of

Need help with this recursive java method.

Using the operations for a Queue defined in class, write a recursive method that receives an instance of a Queue as a parameter, and reverses the contents of the queue.

public static void reverseQueue( Queue aQueue)

Queue class:

import java.util.Arrays; public class ArrayQueueTTH implements QueueInterface{ private Object[] elements; private int maxSize; private int currentSize; private int front; private int rear; public ArrayQueueTTH(int max){ if (max <=0) throw new QueueException("Queue size must be >0"); maxSize =max; currentSize =0; front = rear = 0; elements = new Object[maxSize]; } public boolean isFull(){ // determines whether the queue is full. // Precondition: None. // Postcondition: returns true if the queue is full, and false otherwise. return(currentSize == maxSize); } public boolean isEmpty(){ // determines whether the queue is empty. // Precondition: None. // Postcondition: returns true if the queue is empty, and false otherwise. return (currentSize ==0); } public void enqueue( S newValue){ // adds a new value to the end of the queue // Preconditions: none. // Postconditions: NewValue is inserted at the end of the // queue if (isFull()) throw new QueueException("Queue is full!"); elements[rear] = newValue; currentSize++; rear++; rear %= maxSize; // if (rear == maxSize) rear=0; }// end enqueue public S dequeue(){ // retrieves the value at the front of the queue // Precondition: The queue must not be empty. // Postconditon: If the queue is not empty, the value at // the front of the queue is removed and // returned. //_Errors: returns null if called with an empty queue. if ( isEmpty()) throw new QueueException ("Empty Queue"); S temp = (S)elements[front]; front++; currentSize--; front %= maxSize; return temp; } public S peek(){ // returns the value at the front of the queue without // removing it. // Preconditions: The queue must not be empty. // Postconditon: If the queue is not empty, the value at // the front of the queue is returned. //_Errors: returns null if called with an empty queue. return (S)elements[front]; } public int size(){ // returns the number of elements in the queue. // Preconditions: NONE; // Postconditions: None; return currentSize; } public int getMaxSize(){ return maxSize; } // deletes all elements from the queue public void clear(){ Arrays.fill(elements,null); front = rear= currentSize =0; } public int getFront(){ return front; } public int getRear(){ return rear; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!