Question #245965

Write a menu driven program in Java to perform the operations on an Integer

Queue by making use of the concept of interfaces. And create custom

exceptions to deal with the following situations

A non-integer value is encountered in the queue

b. Insert operation when the Queue if full

c. Remove operation when queue is empty.


Expert's answer

class Queue
{
    private int[] arr;      
    private int front;    
    private int rear;    
    private int capacity; 
    private int count;      
    Queue(int size)
    {
        arr = new int[size];
        capacity = size;
        front = 0;
        rear = -1;
        count = 0;
    }
    public void dequeue() {
        if (isEmpty()) {
            System.out.println("Underflow\nProgram Terminated");
            System.exit(1);
        }
        System.out.println("Removing " + arr[front]);
        front = (front + 1) % capacity;
        count--;
    }
    public void enqueue(int item) {
        if (isFull()) {
            System.out.println("Overflow\nProgram Terminated");
            System.exit(1);
        }
        System.out.println("Inserting " + item);
        rear = (rear + 1) % capacity;
        arr[rear] = item;
        count++;
    }
    public int peek() {
        if (isEmpty()) {
            System.out.println("Underflow\nProgram Terminated");
            System.exit(1);
        }
        return arr[front];
    }
    public int size() {
        return count;
    }
    public Boolean isEmpty() {
        return (size() == 0);
    }
    public Boolean isFull() {
        return (size() == capacity);
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS