Thursday, February 29, 2024
HomeSoftware EngineeringTips on how to make an arithmetic operate in Java

Tips on how to make an arithmetic operate in Java


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 constructive integers, and a will all the time be the primary quantity within the operation, and b all the time the second.

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

A number of 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 {

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

Take a look at circumstances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Take a look at
    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"));
    }
    
    @Take a look at
    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", answer(a,b,op), ArithmeticFunction.arithmetic(a,b,op));
      }
    }
    
    personal static int randInt(int min, int max) {
        return (int)(min + Math.random() * ((max - min) + 1));
    }
    
    personal static int answer(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;
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments