Transcribed TextTranscribed Text

Requirements You will be provided with an almost complete version of the Evaluator class (Evaluator. java). You should program the utility classes it uses Operand and Operator and then follow the suggestions in the code to complete the implementation of the Evaluator class. The Evaluator implements a single public method, eval that takes a single String parameter that represents an infix mathematical expression, parses and evaluates the expression, and returns the integer result. An example expression is2- +3*4, which would be evaluated to 14. The expressions are composed of integer operands and operators drawn from the set -,*,/,^, ( and These operators have the following precedence (# and willbe discussed shortly, and they are extra credit) Operator Priority +, 1 :/ 2 n 3 The algorithm that is partially implemented in eval processes the tokens in the expression string using two Stacks one for operators and one for operands (algorithm reproduced here from Evaluation of infix expression s): an operand token is scanned, an Operand object is created from the token, and pushed to the operand Stack If an operator token is scanned and the operator Stack is empty, then an Operator object is created from the token, and pushed to the operator Stack * If an operator token is scanned, and the operator Stack is not empty, and the operator's precedence is greater than the precedence of the Operator at the top of the Stack, then and Operator object is created from the token, and pushed to the operator Stack * If the token is and Operator object is created from the token, and pushed to the operator tack * the token is) the process Operators until the corresponding (is encountered Pop the (Operato: If none of the above cases apply, process an Operator Processing an Operator means to: Pop the operand Stack twice (for each operand note the order!!) Pop the operator Stack Execute the Operator with the two Operands * Push the result onto the operand Stack When all tokens are read. process Operators until the operator Stack is empty Requirement 1: Implement the above algorithm within the Evalua: tor class (this implementation need not be submitted, but it is strongly recommended that you begin with this version). Requirement 2: Test this implementation with expressions that test all possible cases (you may use the included Eva lua !torTest class to do this or create JUnit tests). Requirement 3: PLEASE SKIP Requirement 4: Implement the following class hierarchy * Operator must be an abstract superclass boolean check( String token ) returns true if the specified token is an operator abstract int priority () returns the precedence of the operator abstract Operand execute( Operand operandOne, Operand operandTwo performs mathematical calculation dependent on its type This class should contain HashMap with all of the Operators stored as values, keyed by their token. An interface should be created in Operator to allow the Evaluator (or other software components in our system) to look up Operators by token. * Individual Operator classes must be subclassed from Operator toimplement each of the operations allowed in our expressions * Operand boolean check( String token ) returns true if the specified token is an operand * Operand String token Constructor Operand( double value Constructor * int getValuel returns the integer value of this operand Requirement 5: Reuse your Evaluator implementation in the provided GUI Calculator (

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

package Evaluator;

import operators.Operator;
import java.util.*;
import operators.LeftParenthesisOperator;
import operators.RightParenthesisOperator;

public class Evaluator {

    private Stack<Operand> operandStack;
    private Stack<Operator> operatorStack;

    private StringTokenizer tokenizer;
    private static final String DELIMITERS = "+-*^/ ()";

    public Evaluator() {
       operandStack = new Stack<>();
       operatorStack = new Stack<>();

    public int eval(String expression) {
       String token;

       // The 3rd argument is true to indicate that the delimiters should be used
       // as tokens, too. But, we'll need to remember to filter out spaces.
       this.tokenizer = new StringTokenizer(expression, DELIMITERS, true);
       // clear stack
       // get ready for the new task

       // initialize operator stack - necessary with operator priority schema
       // the priority of any operator in the operator stack other than
       // the usual mathematical operators - "+-*/" - should be less than the priority
       // of the usual operators
       // TODO Operator is abstract - this will need to be fixed:
       while (this.tokenizer.hasMoreTokens()) {
            // filter out spaces
            if (!(token = this.tokenizer.nextToken()).equals(" ")) {
                // check if token is an operand
                if (Operand.check(token)) {
                   operandStack.push(new Operand(token));
                } else {
                   if (!Operator.check(token)) {
                        System.out.println("*****invalid token******");
                        throw new RuntimeException("*****invalid token******");

                   // TODO Operator is abstract - these two lines will need to be fixed:
                   // The Operator class should contain an instance of a HashMap,
                   // and values will be instances of the Operators. See Operator class
                   // skeleton for an example.
                   //Operator newOperator = new Operator(token);
                   Operator newOperator = Operator.getOperator(token);
                   // left parenthesis case
                   if (newOperator.getClass() == LeftParenthesisOperator.class) {
                   // right parenthesis case
                   else if (newOperator.getClass() == RightParenthesisOperator.class) {
                        boolean isLeftPopped = false;
                        // we are workin on binary operation
                        // so we must have 2 operand
                        while (operandStack.size() >= 2) {
                            // note that when we eval the expression 1 - 2 we will
                            // push the 1 then the 2 and then do the subtraction operation
                            // This means that the first number to be popped is the
                            // second operand, not the first operand - see the following code
                            Operator oldOpr = operatorStack.pop();
                            if (oldOpr.getClass() == LeftParenthesisOperator.class) {
                               // clear left parenthesis
                               isLeftPopped = true;

By purchasing this solution you'll be able to access the following files: and Solution2.docx.

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats