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:

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
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:

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:

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:

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

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;
    }
}