Answer to Question #247636 in Java | JSP | JSF for Jerry

Question #247636
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-07T03:05:09-0400
import java.util.*;
class queue
{
    protected int Queue[] ;
    protected int x, r, s, len;
    public queue(int n) 
    {
        s = n;
        len = 0;
        Queue = new int[s];
        x = -1;
        r = -1;
    }    
    public boolean isEmpty() 
    {
        return x == -1;
    }    
    public boolean isFull() 
    {
        return x==0 && r == s -1 ;
    }    
    public int getSize()
    {
        return len ;
    }    
    public int peek() 
    {
        if (isEmpty())
           throw new NoSuchElementException("Underflow Exception");
        return Queue[x];
    }    
    public void insert(int i) 
    {
        if (r == -1) 
        {
            x = 0;
            r = 0;
            Queue[r] = i;
        }
        else if (r + 1 >= s)
            throw new IndexOutOfBoundsException("Overflow Exception");
        else if ( r + 1 < s)
            Queue[++r] = i;    
        len++ ;    
    }    
    public int remove() 
    {
        if (isEmpty())
           throw new NoSuchElementException("Underflow Exception");
        else 
        {
            len-- ;
            int element = Queue[x];
            if ( x == r) 
            {
                x = -1;
                r = -1;
            }
            else
                x++;                
            return element;
        }        
    }
    public void display()
    {
        System.out.print("\nQueue = ");
        if (len == 0)
        {
            System.out.print("Empty\n");
            return ;
        }
        for (int i = x; i <= r; i++)
            System.out.print(Queue[i]+" ");
        System.out.println();        
    }
}
 
public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
 
        System.out.println("Queue\n");
        System.out.println("Enter an interger for the size of the Queue ");
        int n = input.nextInt();
        queue w = new queue(n);        
        char M;
        do{
            System.out.println("\nChoose the Operation");
            System.out.println("1. insert");
            System.out.println("2. remove");
            System.out.println("3. peek");
            System.out.println("4. isEmpty");
            System.out.println("5. isFull");
            System.out.println("6. size");
            int option = input.nextInt();
            switch (option)
            {
            case 1 : 
                System.out.println("Enter integer element to insert");
                try
                {
                    w.insert( input.nextInt() );
                }
                catch(Exception e)
                {
                    System.out.println("Error : " +e.getMessage());
                }                         
                break;                         
            case 2 : 
                try
                {
                    System.out.println("Element removed = "+w.remove());
                }
                catch(Exception e)
                {
                    System.out.println("Error : " +e.getMessage());
                }
                break;                         
            case 3 : 
                try
                {
                    System.out.println("Peek = "+w.peek());
                }
                catch(Exception e)
                {
                    System.out.println("Error : "+e.getMessage());
                }
                break;                            
            case 4 : 
                System.out.println("Queue Empty = "+w.isEmpty());
                break;                
            case 5 : 
                System.out.println("queue full = "+w.isFull());
                break;                          
            case 6 : 
                System.out.println("Size = "+ w.getSize());
                break;                         
            default : System.out.println("Invalid entry\n ");
                break;
            }
            w.display();            
            System.out.println("\nDo you want to continue (y or n) \n");
            M = input.next().charAt(0);
 
        } while (M == 'Y'|| M == 'y');                                                        
    }    
}

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