New to Nutbox?

Calculator Conundrum

1 comment

drago18121996
68
14 days agoSteemit

class CalculatorConundrum {
public String calculate(int operand1, int operand2, String operation) {
if (operation == null) {throw new IllegalArgumentException ("Operation cannot be null");}
if(operation.equals("")) {throw new IllegalArgumentException ("Operation cannot be empty");}

  double total =0;
  try{switch(operation){
      case "+":
        total = operand1+operand2;
        break;
      case "-":
        total = operand1-operand2;
        break;
      case "*":
        total = operand1*operand2;
        break;
      case "/":
        total = operand1/operand2;
        break;
      default:
          throw new IllegalOperationException (String.format("Operation '%s' does not exist", operation));
  }
      }  catch(ArithmeticException e){throw new IllegalOperationException("Division by zero is not allowed",e);}
  return String.format("%s %s %s = %d", operand1, operation, operand2, (int)total);
}

}

Comments

Sort byBest