import java.util.Scanner; // Scanner is used strictly for testing purposes /* CMPS 144 ** Authors: R. McCloskey and < YOUR NAME > ** Collaborated with: ... ** Known defects: ... */ /* An instance of this class, which is analogous to the java.util.Scanner ** class, acts as a scanner of the arithmetic expression provided to its ** constructor. That is, via calls to its next() method, it provides the ** tokens/elements within the expression, one after the other. There are ** four categories of tokens: ** ** (1) left parenthesis : "(" ** (2) right parenthesis : ")" ** (3) operator symbol: ** (3a) arithmetic operator: Any of "+", "-", "*", or "/" ** (3b) relational operator: Any of "==", "!=", "<", "<=", ">", ">=" ** (4) integer literal: Any maximal sequence of the digit characters '0'..'9'. ** ** The hasNextX() (X = LeftParenthesis, Operator, etc.) methods allow the ** client to determine whether the next token (i.e., that to be returned ** by a call to next()) is in category X. */ public class ArithExprScanner { // class constants // --------------- //RWM: Suggested // Declarations of constants of type int having distinct values, // each one serving as the code for a particular category of token. // instance variables //RWM: Suggested // ------------------ private final int N; // length of the expression to be scanned private String expr; // the arithmetic expression to be scanned. private int left, right; // the next token is expr.substring(left..right) private int codeOfNext; // code indicating in which category the next // token lies // constructor // ----------- /* Initializes this ArithExprScanner object in preparation for scanning ** the given expression. */ public ArithExprScanner(String expression) { } // STUB! // observers // --------- /* Reports whether or not there is a next token. */ public boolean hasNext() { return true; } // STUB! /* Reports whether or not the next token is a left parenthesis. ** pre: hasNext() */ public boolean hasNextLeftParenthesis() { return true; } // STUB! /* Reports whether or not the next token is a right parenthesis. ** pre: hasNext() */ public boolean hasNextRightParenthesis() { return true; } // STUB! /* Reports whether or not the next token is an operator ** pre: hasNext() */ public boolean hasNextOperator() { return true; } // STUB! /* Reports whether or not the next token is an arithmetic operator ** pre: hasNext() */ public boolean hasNextArithOperator() { return true; } // STUB! /* Reports whether or not the next token is a relational operator ** pre: hasNext() */ public boolean hasNextRelatOperator() { return true; } // STUB! /* Reports whether or not the next token is an integer literal. ** pre: hasNext() */ public boolean hasNextIntLit() { return true; } // STUB! /* Reports whether or not the next token is of an unknown kind. ** pre: hasNext() */ public boolean hasNextUnknown() { return false; } // STUB! // observer/mutator // ---------------- /* Returns the next token and advances past it. ** pre: hasNext() */ public String next() { String result = expr.substring(left, right); left = right; findNext(); return result; } // utilities // --------- //RWM: Suggested /* Advances the values of instance variables 'left' and 'right' so that ** expr.substring(left,right) is the token that should be returned by ** the next call to next() (unless there is no such token, in which ** case 'left' should have value expr.length()). In addition, the ** value of 'codeOfNext' is updated to identfy the category of the ** next token. */ private void findNext() { } // main() method for testing purposes // ---------------------------------- public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an arithmetic expression: "); String response = input.nextLine(); ArithExprScanner aes = new ArithExprScanner(response); while (aes.hasNext()) { String remark; if (aes.hasNextLeftParenthesis()) { remark = ": left parenthesis"; } else if (aes.hasNextRightParenthesis()) { remark = ": right parenthesis"; } else if (aes.hasNextArithOperator()) { remark = ": arithmetic operator"; } else if (aes.hasNextRelatOperator()) { remark = ": relational operator"; } else if (aes.hasNextIntLit()) { remark = ": integer literal"; } else if (aes.hasNextUnknown()) { remark = ": unknown"; } else { remark = ": impossible !!!!"; } System.out.println(aes.next() + remark); } } }