Question: java problems Show the result that will be displayed on the screen for each of the following two calls to the method dump in the

java problems

Show the result that will be displayed on the screen for each of the following two calls to the method dump in the code fragment below. The class CircularQueue can be found below.

CircularQueue q; q = new CircularQueue(4);

int i=0; while (!q. isFull ()){

i ++; q.enqueue(new Integer(i));

}

if (!q.isEmpty()){

q . dequeue ( ) ;

}

if (!q.isEmpty()){

q . dequeue ( ) ;

}

while (!q. isFull ()){

i ++;

q.enqueue(new Integer(i));

}

q.dump();

while (!q.isEmpty()){

q . dequeue ( ) ;

}

q.dump();

Class CircularQueue

public class CircularQueue implements Queue {

public static final int DEFAULTCAPACITY = 100;

private final int MAX QUEUE SIZE; private E[] elems;

private int front , rear ;

public CircularQueue () { t h i s (DEFAULT CAPACITY ) ;

} public CircularQueue(int capacity) {

if (capacity < 0) {

throw new IllegalArgumentException ( Intrger.toString(capacity)

}

MAX QUEUE SIZE = capacity ; elems = new E[MAXQUEUESIZE]; front = 0; rear = 1; // Represents the empty queue

}

public boolean isEmpty () {

return ( rear == 1);

} public boolean isFull () {

return ! isEmpty () && nextIndex(rare)= front;

}

{

private int nextIndex(int index) {

return ( index + 1) % MAX QUEUE SIZE;

}

public void dump() { System.out.println(MAXQUEUESIZE = + MAXQUEUESIZE);

System.out.println(front = + front);

System.out.println(rear = + rear); for(int i = 0; i < elems.length; i++){

System.out.print( elems[+i+] = );

if (elems[i] == null) {

System.out. println( null );

}else{

System.out.println( elems[ i ] );

}

}

System.out. println ();

}

public void enqueue(E o) {

if (o == null) {

throw new IllegalArgumentException( null );

} if (isFull()){

throw new QueueOverflowException ( ) ;

}

rear = nextIndex(rear);

elems[rear] = o;

}

public E dequeue () {

if (isEmpty()) {

throw new EmptyQueueException();

}

E result = elems[front]; elems[front] = null; // scrubbing

if (front == rear) { // Following this call to dequeue

front = 0; // the queue will be empty rear = 1;

}else{ front = nextIndex(front);

}

return result ;

}

}

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!