import java.util.Stack; import java.util.Scanner; /* Java class that includes two public methods that pertain to arithmetic ** expressions in prefix form. One method determines whether or not a ** given expression is syntactically valid. The other evaluates a given ** prefix expression. ** ** Assumed here is that expressions are given in the form of a String ** each token of which is either an unsigned integer literal (e.g., a ** sequence of one or more decimal digits) or one of the operator symbols ** '+', '-', '*', or '/'. Tokens must be separated by whitespace. ** ** For: CMPS 144, Prog. Assg. #4 ** Authors: R. McCloskey and < STUDENT's NAME > ** Collaborated with: ... ** Known Defects: ... */ public class PrefixExprEvaluator { // class constants // --------------- public static final String PLUS = "+"; public static final String MINUS = "-"; public static final String TIMES = "*"; public static final String DIVIDED_BY = "/"; // methods of interest // ------------------- /* Reports whether or not the given string is a syntactically ** valid (arithmetic) prefix expression, which is to say that it ** satisfies these conditions: ** (1) Every token is an unsigned integer literal or an operator. ** (2) The number of occurrences of integer literals exceeds the ** number of occurrences of operators by exactly one. ** (3) In every proper prefix of the expression, the number of ** occurrences of integer literals is less than or equal to ** the number of occurrences of operators. ** ** It is assumed that tokens are separated from each other by ** whitespace. */ public boolean isValid(String expr) { return false; // STUB } /* Evaluates the given arithmetic expression, which is assumed ** to be syntactically valid and in prefix form. ** pre: isValid(prefixExpr) */ public int valueOf(String prefixExpr) { return -999; // STUB } // private utility methods // ----------------------- /* Reports whether or not the given string is equal to one of the ** operator symbols. */ private boolean isOperator(String s) { return s.equals(PLUS) || s.equals(MINUS) || s.equals(TIMES) || s.equals(DIVIDED_BY); } /* Returns the given string enclosed in double quotes. */ private String quoted(String s) { final char DOUBLE_QUOTE = '\"'; return DOUBLE_QUOTE + s + DOUBLE_QUOTE; } }