Divide By Zero Exception
807603Feb 12 2008 — edited Feb 13 2008Hi this is what I'm trying to do......
public void divide()
throws StackEmptyException,
CalculatorDivideByZeroException
Replaces the top two elements of the stack with the quotient, that is, with the result of dividing the second-to-top element by the top element. This is integer division--the remainder is discarded. Throws a StackEmptyException if the stack contains fewer than two elements.
Throws:
StackEmptyException
CalculatorDivideByZeroException
Can anyone tell me how to proceed?
I have implemented other methods like push, pop, peek, isEmpty, add, subtract. This is the one I'm having probelm with. When I try to divide by 0, there is an error.
public void divide () throws StackEmptyException, CalculatorDivideByZeroException {
try {
BigInteger x = pop();
BigInteger y = pop();
BigInteger temp;
if ( y.intValue()==0)
{
throw new CalculatorDivideByZeroException ();
}
temp = x.divide(y);
push (temp);
} catch (StackEmptyException e) {
System.out.println(e.toString());
System.out.println("Fewer than 2 elements in stack");
} catch(StackFullException e) {
System.out.println(e.toString());
System.out.println("Stack is full ");
}
return;
}