Advertisement
advictoriam

Untitled

Jan 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. public class Numbers
  2. {
  3.    /**
  4.       Combines two numbers with a given operator.
  5.       @param op one of "+", "-", "*", "/", or "^" (for power)
  6.       @param first the first argument
  7.       @param second the second argument
  8.       @return the result of evaluating the operator with the arguments
  9.    */
  10.    public static double evaluate(String op, double first, double second)
  11.    {
  12.       switch(op)
  13.       {
  14.       case "+":
  15.          return first + second;
  16.       case "-":
  17.          return first - second;
  18.       case "*":
  19.          return first * second;
  20.       case "/":
  21.          return first / second;
  22.       case "^":
  23.          return Math.pow(first, second);
  24.       }
  25.       return -1.0;
  26.    }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement