Answer to Question #245965 in Java | JSP | JSF for kishan

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.


1
Expert's answer
2021-10-03T11:45:41-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS