Can any one help me to solve my problem with these coding. I did almost every pa
ID: 3530978 • Letter: C
Question
Can any one help me to solve my problem with these coding. I did almost every part but but I am having hard time with print method and condition for Exception in enqueue method. I am not supposed to add or alter any member or method to the program. These are all methods and members that can use. There are some comment on the print which i supposed to fill in
public class CircularQueue {
private int [] queue;
private int front, rear;
// do not change the constructor
CircularQueue() {
queue = new int [3];
front = 0;
rear = -1;
}
// FILL IN:
// throws DSException if out of space
public void enqueue ( int item ) throws DSException {
if((rear+1)- front==queue.length){
throw new DSException("Queue is full");
}
if(rear==queue.length-1){
rear=-1;
}
queue[rear+1] = item;
rear = (rear+1)%queue.length;
}
// FILL IN:
// throws DSException if no element in the queue
// return the dequeued value
public int dequeue () throws DSException {
if(rear==-1 && front ==0) throw new DSException("Queue is empty");
int temp = queue[front];
queue[front]=0;
front = (front+1)%queue.length;
return temp;
}
// FILL IN:
// return the value at beginning of the queue
// throws DSException if no element in the queue
public int first () throws DSException {
if(rear==-1)
throw new DSException("Queue is empty.");
else
return queue[front];
}
// FILL IN:
// print the circular queue in the following format
// - print "+" before the value at the front
// - print "-" after the value at the rear
// - print "." for the places without valid element.
public void print () {
System.out.print(" <");
if(front==0 && rear ==-1){
for(int i =0; i<queue.length; i++){
System.out.print(" . ");
}
}else
for ( int i = 0; i < queue.length; i++ ){
if ( i == front ) {
System.out.print(" +" + queue[i] + " ");
} else if ( i == rear ) {
System.out.print( queue[i]+ "-" + " " );
}else if (rear==6 ){
System.out.print(" " + " +"+ queue[i] +"- " + " " );}
else {
System.out.print( queue[i]+ " " );
}
}
System.out.print(" > ");
}
}
Correct OutPut for the program:
EMPTY:
<. . . . . >
ENQUEUE (0):
<+0- . . . . >
ENQUEUE (1):
<+0 1- . . . >
ENQUEUE (2):
<+0 1 2- . . >
ENQUEUE (3):
<+0 1 2 3- . >
ENQUEUE (4):
<+0 1 2 3 4- >
DEQUEUE() = [0]:
<. +1 2 3 4- >
DEQUEUE() = [1]:
<. . +2 3 4- >
DEQUEUE() = [2]:
<. . . +3 4- >
DEQUEUE() = [3]:
<. . . . +4- >
ENQUEUE (5):
<5- . . . +4 >
ENQUEUE (6):
<5 6- . . +4 >
ENQUEUE (7):
<5 6 7- . +4 >
DEQUEUE() = [4]:
<+5 6 7- . . >
DEQUEUE() = [5]:
<. +6 7- . . >
DEQUEUE() = [6]:
<. . +7- . . >
DEQUEUE() = [7]:
<. . . . . >
ENQUEUE (8):
<. . . +8- . >
ENQUEUE (9):
<. . . +8 9- >
ENQUEUE (10):
<10- . . +8 9 >
ENQUEUE (11):
<10 11- . +8 9 >
Explanation / Answer
Problem with circular arrays and front/rear indices is that 'full' and 'empty' are indistinguishable. You will have to add a boolean 'empty', which is initially true, and is used in the tests.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.