Calculator dividing by 0 problem.
New assignment, need to obviously make the number variables and action variables work together. All of them do apart from divide which has a major problem that I can't figure out how to solve :(
Code :
class Calculator
{
private long count = 0;
public void evaluate( char action, long number ){
if ( action == '+' ){
count = count + number;
}
else if ( action == '-' ){
if ( number == 0) {
count = count;
}
count = count - number;
}
else if ( action == '/' ){
if ( number == 0 ){
count = count;
}
count = count / number;
}
else if ( action == '*' ){
count = count * number;
}
}
public long getValue(){
return count;
}
public void setValue( long number ){
count = number;
}
public void reset(){
count = 0;
}
}
Yet again advice/reading material would be very helpful :)
Re: Calculator dividing by 0 problem.
What is the problem?
If you are getting a runtime exception, post it along with the (runnable) code that gives rise to the exception. Indicate, somehow, which line(s) of your code the stack trace is referring to.
-----
I am a bit confused by what you have written for subtraction:
Code :
else if ( action == '-' ){
if ( number == 0) {
count = count;
}
count = count - number;
}
What is that code supposed to do? Why do you assign the value of count to itself (an operation that does nothing) before performing the subtraction? Something similar could be asked of the division code.
Re: Calculator dividing by 0 problem.
Sorry man, I was in a hurry for work and posted the wrong code. Il post my Logic and Calculator code along with the exception.
My assignment requires that number / zero will need to return as 0. My program breaks when I try this:
Calculator:
Code :
class Calculator
{
private long count = 0;
public void evaluate( char action, long number ){
if ( action == '+' ){
count = count + number;
}
else if ( action == '-' ){
count = count - number;
}
else if ( action == '/' ){
count = count / number;
}
else if ( action == '*' ){
count = count * number;
}
}
public long getValue(){
return count;
}
public void setValue( long number ){
count = number;
}
public void reset(){
count = 0;
}
}
Logic:
Code :
class Logic
{
private enum State { FIRST_NUMBER, SUBSEQUENT_NUMBER };
private State state = State.FIRST_NUMBER;
private long number = 0;
private char op = ' ';
private Calculator calc = null;
public Logic( Calculator calculator )
{
calc = calculator;
}
public String process( String button )
{
String info = null;
if ( button.length() == 1 )
{
char c = button.charAt(0);
if ( c >= '0' && c <= '9' ) // Digit
{
number = number * 10 + c-'0'; // Build number
} else {
switch ( c )
{
case 'C' : number = 0;
break;
case '=' :
case '+' : case '-' :
case '*' : case '/' :
switch ( state )
{
case FIRST_NUMBER:
calc.setValue( number );
state = State.SUBSEQUENT_NUMBER;
break;
case SUBSEQUENT_NUMBER:
if ( op != '=' )
calc.evaluate( op, number );
break;
}
op = c; number = 0;
break;
}
}
} else {
if ( button.equals( "CR" ) ) // Clear Result
{
calc.reset(); number = 0; state = State.FIRST_NUMBER;
}
}
return info;
}
public long getResult()
{
return calc.getValue();
}
public long getNumber()
{
return number;
}
}
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
at Calculator.evaluate(Calculator.java:12)
at Logic.process(Logic.java:40)
at GUI$ButtonPress.actionPerformed(GUI.java:106)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6504)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:626 9)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713 )
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
Re: Calculator dividing by 0 problem.
Quote:
count = count / number;
This is the line which is triggering the exception.
This code would throw the / zero exception if number = 0, so I suggest putting in place a simple if-statement to check for that possibility before attempting the division. :)