Create an appropriate Java Infix calculator class (Carrano & Henry, 2019, p XXXXXXXXXXYour program should accept at least five elements (data1, data2, data3, data4, data5) and will perform an...

1 answer below »

Create an appropriate Java Infix calculator class (Carrano & Henry, 2019, p. 176). Your program should accept at least five elements (data1, data2, data3, data4, data5) and will perform an appropriate Infix operation given the following scenarios:



  • data1 data + data3 * data4 –

  • data1 data * data3 * data1 – / data4 data5* +


Use the following values for each input:


data1 = 1;
data2 = 2;
data3 = 4
data4 = 5
data5 = 3


Ensure that your program has the required class and a test class

Answered Same DayJan 12, 2021

Answer To: Create an appropriate Java Infix calculator class (Carrano & Henry, 2019, p XXXXXXXXXXYour program...

Neha answered on Jan 15 2021
144 Votes
49477/InfixEvaluator.java
49477/InfixEvaluator.java
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Scanner;
public class InfixEvaluator
{
    publi
c double infix(String exprn)
    {
        //remove white space and add evaluation operator
        exprn=exprn.replaceAll("[\t\n ]", "")+"=";
        String operator="*/+-=";
        //split up the operators from the values
        StringTokenizer tokenizer=new StringTokenizer(exprn, operator, true);
        Stack operatorStack=new Stack();
        Stack valueStack=new Stack();
        while(tokenizer.hasMoreTokens())
        {
            //add the next token to the proper stack
            String token=tokenizer.nextToken();
            if(operator.indexOf(token)<0)
                valueStack.push(token);
            else
                operatorStack.push(token);
            //perform any pending operations
            resolve(valueStack, operatorStack);
        }
        //return the top of the value stack
        String lastOne=(String)valueStack.pop();
        return Double.parseDouble(lastOne);
    }

    public int getPriority(String op)
    {
        if(op.equals("*") || op.equals("/"))
            return 1;
        else if(op.equals("+") || op.equals("-"))
            return 2;
        else if(op.equals("="))
            return 3;
        else
            return Integer.MIN_VALUE;
    }

    public void resolve(Stack values, 
            Stack operators)
    {
        while(operators.size()>=2)
        {
            String first=(String)operators.pop();
            String second=(String)operators.pop();
            if(getPriority(first)            {
                operators.push(second);
                operators.push...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here