import java.io.*;
public class MainClass
{
public static void main(String args[])
{
Queue queue=new Queue(5);
try
{
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
//queue.push(6);
queue.display();
queue.pop();
queue.pop();
queue.pop();
queue.pop();
queue.pop();
queue.pop();
}
catch(QueueException e)
{
e.Error();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Queue is empty");
}
}
}
/*Queue.java*/
import java.io.*;
public class Queue
{
int front;
int rear;
int size;
int Array[];
public Queue(int s)
{
front=0;
rear=-1;
size=s;
Array=new int[size];
System.out.println("Inserted : " + ele);
}
public void push(int ele)throws QueueException
{
if(rear==size-1)
throw new QueueException();
else
{
Array[++rear]=ele;
}
}
public void pop()
{
if(rear==-1)
{
throw new ArrayIndexOutOfBoundsException();
}
System.out.println("Deleted : " + ++front);
if(front>rear)
{
front=0;
rear=-1;
}
}
public void display()
{
System.out.println("elements in queue");
for(int i=front;i<=rear;i++)
System.out.println(Array[i]+" ");
System.out.println(); }}
/*QueueException*/
import java.io.*;
public class QueueException extends Exception
{
public void Error()
{
System.out.println("Queue is full");
}
}
Saturday, September 17, 2016
6. JAVA program to implement a Queue using user defined Exception Handling
Labels:
JAVA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment