how to use exceptions in a queue

Monday, May 5, 2014










Take the 2-minute tour ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.



















public class ArrayQueue{
private Object[] theArray;
private int currentSize;
private int front;
private int rear;
static final int DEFAULT_CAPACITY=10;
public ArrayQueue(){
theArray=new Object[DEFAULT_CAPACITY];
makeEmpty();
}
public void makeEmpty(){
currentSize=0;
rear=-1;
front=0;
}

public void enqueue(Object x) throws OverFlow{
if (isFull())
throw new OverFlow("Array size exceeded");
else{
rear=increment(rear);
theArray[rear]=x;
currentSize++;
}
}

public Object dequeue()throws UnderFlow{
if (isEmpty())
throw new UnderFlow("Empty array");
else{
Object returnValue=theArray[front];
theArray[front]=null;//check if this has to be done
front=increment(front);
currentSize--;
return returnValue;
}
}

public Object getFront() throws UnderFlow{
if (isEmpty())
throw new UnderFlow("Empty array");
else
return theArray[front];

}

public boolean isEmpty(){
if (currentSize==0)
return true;
else
return false;
}

public boolean isFull(){
if (currentSize==theArray.length)
return true;
else
return false;
}

public int increment(int x){
if (x+1==currentSize)
x=0;
else
x++;
return x;
}

public static void main (String args[]){
ArrayQueue q=new ArrayQueue();
q.enqueue("1");
}


}



public class OverFlow extends Exception{
public OverFlow(){
super();
}
public OverFlow(String s){
super(s);

}
}


public class UnderFlow extends Exception{
public UnderFlow(){
super();
}
public UnderFlow(String s){
super(s);

}
}


When I try to run this I get an error as unreported exception OverFlow,Must be caught or declared to be thrown.

I am new to Java and programming but I have to learn a data structures course.Therefore if someone can tell me whats wrong here and how to correct it it would be really helpful
















lang-java









http://ift.tt/1htjCD0