/* This class defines Strings that denote the four basic arithmetic ** operators and has a method that returns the result of applying a ** given operator to two given integers. ** ** Author: R. McCloskey ** Date: Feb. 2022 */ public class Operator { // class constants // --------------- public static final String PLUS = "+"; public static final String MINUS = "-"; public static final String TIMES = "*"; public static final String DIVIDEDBY = "/"; /* Returns the number resulting from applying the given operator to ** the given operands. */ public static int apply(int left, String operator, int right) { if (operator.equals(PLUS)) { return left + right; } else if (operator.equals(MINUS)) { return left - right; } else if (operator.equals(TIMES)) { return left * right; } else if (operator.equals(DIVIDEDBY)) { return left / right; } else { String errorMessage = " is not a legal operator."; throw new IllegalArgumentException(operator + errorMessage); } } /* Reports whether the given string describes one of the operators ** supported by this class. */ public static boolean isOperator(String str) { return str.equals(PLUS) || str.equals(MINUS) || str.equals(TIMES) || str.equals(DIVIDEDBY); } }