Created
June 9, 2019 12:37
-
-
Save joshiraez/e4b6781afdf34c137b632d2b83cbe206 to your computer and use it in GitHub Desktop.
Switch implementation of a simple Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package behaviouralSwitch.sentence; | |
public class Calculator { | |
public int calculate(char op, int n1, int n2) { | |
int result; | |
switch(op) { | |
case '+': | |
result = n1 + n2; | |
break; | |
case '-': | |
result = n1 - n2; | |
break; | |
case 'x': | |
result = n1 * n2; | |
break; | |
case '/': | |
result = n1 / n2; | |
break; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment