## The challenge

Given two numbers and an arithmetic operator (the name of it, as a string), return the result of the two numbers having that operator used on them.

`a` and `b` will both be positive integers, and `a` will always be the first number in the operation, and `b` always the second.

The four operators are “add”, “subtract”, “divide”, “multiply”.

A few examples:

 ``````1 2 3 4 `````` ``````arithmetic(5, 2, "add") => returns 7 arithmetic(5, 2, "subtract") => returns 3 arithmetic(5, 2, "multiply") => returns 10 arithmetic(5, 2, "divide") => returns 2.5 ``````
 ``````1 2 3 4 `````` ``````ArithmeticFunction.arithmetic(5, 2, "add") => returns 7 ArithmeticFunction.arithmetic(5, 2, "subtract") => returns 3 ArithmeticFunction.arithmetic(5, 2, "multiply") => returns 10 ArithmeticFunction.arithmetic(5, 2, "divide") => returns 2 ``````

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 `````` ``````class ArithmeticFunction { public static int arithmetic(int a, int b, String operator) { switch(operator) { case "add": return a+b; case "subtract": return a-b; case "multiply": return a*b; case "divide": return a/b; } return 0; } } ``````

Option 2:

 ``````1 2 3 4 5 `````` ``````class ArithmeticFunction { public static int arithmetic(int m, int n, String s) { return s == "add" ? m + n : s == "multiply" ? m * n : s == "subtract" ? m - n : m / n; } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 `````` ``````class ArithmeticFunction { private static enum Operation { add { @Override int apply(int a, int b) { return a + b; } }, subtract { @Override int apply(int a, int b) { return a - b; } }, multiply { @Override int apply(int a, int b) { return a * b; } }, divide { @Override int apply(int a, int b) { return a / b; } }; abstract int apply(int a, int b); } public static int arithmetic(int a, int b, String operator) { return Operation.valueOf(operator).apply(a, b); } } ``````

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testBasic() { assertEquals("'add' should return the two numbers added together!", 3, ArithmeticFunction.arithmetic(1, 2, "add")); assertEquals("'subtract' should return a minus b!", 6, ArithmeticFunction.arithmetic(8, 2, "subtract")); assertEquals("'multiply' should return a multiplied by b!", 10, ArithmeticFunction.arithmetic(5, 2, "multiply")); assertEquals("'divide' should return a divided by b!", 4, ArithmeticFunction.arithmetic(8, 2, "divide")); } @Test public void testRandom() { String[] commands = new String[]{"add","subtract","multiply","divide"}; for (int i = 0; i < 40; i++) { int a = randInt(0, 10); int b = randInt(1, 10); String op = commands[randInt(0,3)]; assertEquals("It should work for random inputs too", solution(a,b,op), ArithmeticFunction.arithmetic(a,b,op)); } } private static int randInt(int min, int max) { return (int)(min + Math.random() * ((max - min) + 1)); } private static int solution(int a, int b, String operator) { if (operator.equals("add")) return a + b; if (operator.equals("subtract")) return a - b; if (operator.equals("multiply")) return a * b; return a / b; } } ``````