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
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started