How one can make an arithmetic operate in Java

[ad_1]

The problem

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

a and b will each be optimistic integers, and a will at all times be the primary quantity within the operation, and b at all times the second.

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

Just 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 answer in Java code

Choice 1:

class ArithmeticFunction {
  public static int arithmetic(int a, int b, String operator) {
    change(operator) {
        case "add":
          return a+b;        
        case "subtract":
          return a-b;
        case "multiply":
          return a*b;
        case "divide":
          return a/b;
    }
    return 0;
  }
}

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

Choice 3:

class ArithmeticFunction {

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

    summary int apply(int a, int b);
  }

  public static int arithmetic(int a, int b, String operator) {
    return Operation.valueOf(operator).apply(a, b);
  }
}

Check circumstances to validate our resolution

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Check
    public void testBasic() {
        assertEquals("'add' ought to return the 2 numbers added collectively!", 3, ArithmeticFunction.arithmetic(1, 2, "add"));
        assertEquals("'subtract' ought to return a minus b!", 6, ArithmeticFunction.arithmetic(8, 2, "subtract"));
        assertEquals("'multiply' ought to return a multiplied by b!", 10, ArithmeticFunction.arithmetic(5, 2, "multiply"));
        assertEquals("'divide' ought to return a divided by b!", 4, ArithmeticFunction.arithmetic(8, 2, "divide"));
    }
    
    @Check
    public void testRandom() {
      String[] instructions = 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 = instructions[randInt(0,3)];
        assertEquals("It ought to work for random inputs too", resolution(a,b,op), ArithmeticFunction.arithmetic(a,b,op));
      }
    }
    
    non-public static int randInt(int min, int max) {
        return (int)(min + Math.random() * ((max - min) + 1));
    }
    
    non-public static int resolution(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;
    }
}

[ad_2]

Leave a Reply