-4

I have created a Java calculator, however i need to add code for when the number is divided by zero i set the result to be zero. Everything else is working i just don't know where to add this statement.

heres the code:

public class Calculator
{
// Declaration of a long variable to hold the stored result

private long theResult = 0;  
private long zero = 0;

// Evaluate an arithmetic operation on the stored result
//  E.g evaluate( '+', 9) would add 9 to the stored result
//      evaluate( '/', 3) would divide the stored result by 3
//      actions are '+'. '-', '*', '/'
// Note: if the operation is
//      evaluate( '/', 0 ) the theResult returned must be 0
//      (Not mathematically correct)
//      You will need to do a special check to ensure this
/**
 * perform the operation 
 *  theResult = theResult 'action' number
 * @param action An arithmetic operation + - * /
 * @param number A whole number
 */
public void evaluate( char action, long number)
{

    if (action == '+'){
        theResult += number;
    }

    else if (action == '-'){
        theResult -= number;
    }

    else if (action == '*'){
        theResult *= number;
    }


    else if (action == '/'){
        theResult /= number;
    }


}



/**
 * Return the long calculated value
 * @return The calculated value
 */
public long getValue()
{
    return theResult;
}

/**
 * Set the stored result to be number
 * @param number to set result to.
 */
public void setValue( long number )
{
    this.theResult = number;
}

/**
 * Set the stored result to be 0
 */
public void reset()
{
    if ( theResult != 0) theResult = 0; 
    // im not sure this is correct too
}

}
shmosel
  • 49,289
  • 6
  • 73
  • 138
RHH
  • 41
  • 1
  • 9
  • 2
    Without your actual code, I would guess an `if` before the actual division. Check is divisor is 0. If it is, result is 0. Otherwise, do division. – AntonH Feb 27 '18 at 18:00
  • After edit, it would be in the block `if (action == '/') {...}`, before the actual division is done. – AntonH Feb 27 '18 at 18:02
  • how would i make a check to see if someone divides by zero here then – RHH Feb 27 '18 at 18:07
  • check the input or catch the exception ( ArithmeticException ) – whoopdedoo Mar 24 '18 at 03:59

2 Answers2

2

You would simply nest an if statement within your existing one, like this:

 else if (action == '/'){
    if (number == 0){ //this is the start of the nested if statement
       theResult = 0; //alternatively, you can just type "continue;" on this line since it's 0 by default. 
    }
    else {
       theResult /= number;
    }
}
Scicrazed
  • 542
  • 6
  • 20
2

There are two ways to do this. The first would be something like:

else if (action == '/') {
    if( number == 0 )
        theResult = 0;
    else
        theResult /= number;
}

Another option assumes that you've learned about exceptions:

else if (action == '/') {
    try {
        theResult /= number;
    }
    catch( ArithmeticException ae ) {
        // possibly print the exception
        theResult = 0;
    }
}
stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • i hadn't learnt about exceptions but this is what was appearing in the error messages before so tried to look into it, thankyou this makes sense too – RHH Feb 27 '18 at 18:13