Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What is the difference between mediation and arbitration?

Answered: 1 week ago

Question

How could assessment be used in an employee development program?

Answered: 1 week ago