Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the class Queue to include appropriate error messages if invalid conditio

ID: 3629634 • Letter: M

Question

Modify the class Queue to include appropriate error messages if invalid conditions occur. For example, trying to dequeue an item when the queue is empty.

Here is the Main:
//Week 6 - lecture: driver program (queue)

public class Main
{
public static void main(String[] args)
{
Queue q = new Queue();

System.out.println("Insertion of 10 characters in q");
for (int i = 0; i < 10; i++)
{
int x = 32 + (int)(Math.random()*95);
System.out.println(x + " --> " + (char)x);
q.enqueue((char)x);
}

System.out.println(" Displaying and deleting elements");
for (int i = 0; i < 10; i++)
{
System.out.println("Item at the front: " + q.getFront());

q.dequeue();
}
}
}
Here is the Queue :
//week 6 - lecture: Queue class

public class Queue
{
public Queue()
{
size = 100;
list = new char[size];

front = 0;
back = size-1;

count = 0;
}

public Queue(int s)
{
size = s;
list = new char[size];

front = 0;
back = size-1;

count = 0;
}

public void enqueue(char c)
{
back = (back+1)%size;
list[back] = c;
count++;
}

public void dequeue()
{
front = (front+1)%size;
count--;
}

public char getFront()
{
return list[front];
}

public boolean isEmpty()
{
return count==0;
}

private char[] list;
private int size;
private int count;
private int front, back;
}

Explanation / Answer

If you look at my response to your Java Stack problem - you'll see that prett ymuch the same holds for here. I suggest you give that a read before you finish looking at this.
Invalid conditions can occur in the following operations:
dequeue : if the queue is empty
getFront : if the queue is empty
enqueue : if the queue is full
To check the state of the queue, we already have isEmpty() that returns true if the queue is empty and false otherwise. We can add isFull() ourselves to return true when the queue is full.
================================================================
The Main class remains the same, but the initialized queue is given a smaller size to trigger invalid situations for us.

public static void main(String[] args) {
        Queue q = new Queue(8);

        System.out.println("Insertion of 10 characters in q");
        for (int i = 0; i < 10; i++) {
            int x = 32 + (int) (Math.random() * 95);
            System.out.println(x + " --> " + (char) x);
            q.enqueue((char) x);
        }

        System.out.println(" Displaying and deleting elements");
        for (int i = 0; i < 10; i++) {
            System.out.println("Item at the front: " + q.getFront());

            q.dequeue();
        }
    }


The Queue class is modified as follows:

public class Queue {

    public Queue() {
        size = 100;
        list = new char[size];
        front = 0;
        back = size - 1;
        count = 0;
    }

    public Queue(int s) {
        size = s;
        list = new char[size];
        front = 0;
        back = size - 1;
        count = 0;
    }

    public void enqueue(char c) {
        if(isFull())
            System.out.println("Cannot perform Enqueue: Queue is Full.");
        else
        {
            back = (back + 1) % size;
            list[back] = c;
            count++;
        }
    }

    public void dequeue() {
        if(isEmpty())
            System.out.println("Cannot perform Dequeue: Queue is Empty.");
        else
        {
            front = (front + 1) % size;
            count--;
        }
    }

    public char getFront() {
        if(isEmpty())
        {
            System.out.println("Cannot perform getFront: Queue is Empty.");
            return ' ';
        }
        else
            return list[front];
    }

    public boolean isEmpty() {
        return count == 0;
    }
    public boolean isFull() {
        return count == size;
    }
    private char[] list;
    private int size;
    private int count;
    private int front, back;

    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote