Skip to content

Instantly share code, notes, and snippets.

@joshiraez
Created June 9, 2019 12:37
Show Gist options
  • Save joshiraez/e4b6781afdf34c137b632d2b83cbe206 to your computer and use it in GitHub Desktop.
Save joshiraez/e4b6781afdf34c137b632d2b83cbe206 to your computer and use it in GitHub Desktop.
Switch implementation of a simple Calculator
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