Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Provide me all necssary Screenshort, Commentds and make all class are sep

ID: 3740285 • Letter: P

Question

Please Provide me all necssary Screenshort, Commentds and make all class are separate with highlight:(I use eclipse)

Requirements

The purpose of this project is to gain an understanding of using recursion to solve problems.

Arithmetic Expression Evaluator

Write a Java application or applet that asks a user to enter an arithmetic expression, and then compute and print out the result. The expression may contain parenthesis, *, /, +, - and numbers. Unary plus and minus do not need to be supported. Normal Java evaluation precedence should be used. For example:

(1 + 2) * (6 / 2 + 3) = 18

BNF (Backus-Naur Form) is commonly used in compilers to describe the language to be accepted. A BNF definition of an arithmetic expression is:

<expression> ::= <term> + <expression> |

<term> - <expression> |

<term>

<term>      ::= <factor> * <term> |

               <factor> / <term> |

     <factor>

<factor>   ::= (<expression>) |

   <float> |

   <int>

You may use the above BNF definition as a basis for your implementation. The BNF definition should show you how to call the various functions recursively if you name your functions expression, term and factor. You may use Google searches to help with this project, but you may not use any code found by these searches.

Explanation / Answer

1. ArithmeticExpressionEvaluator.java

import java.util.Stack;

public class ArithmeticExpressionEvaluator {

public static int evaluateArithmeticExpression(String expression) {

// first split the input string into array of tokens that will be parsed in the program

char[] tokens = expression.toCharArray();

// Declare a Stack called values to store integral numbers, obtained by parsing string

Stack<Integer> values = new Stack<Integer>();

// Declare a Stack called values to store different operators, obtained by parsing string

Stack<Character> ops = new Stack<Character>();

for (int i = 0; i < tokens.length; i++) {

// If the current token is a whitespace, we need to skip it

if (tokens[i] == ' ')

continue;

// If the current token is a number, push it to stack for numbers

if (tokens[i] >= '0' && tokens[i] <= '9') {

StringBuffer sbuf = new StringBuffer();

// There may be more than one digits in number, ie. if its a multi-digit number

while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9')

sbuf.append(tokens[i++]); // keep appending all the digits to form the number

values.push(Integer.parseInt(sbuf.toString())); // finally push the obtained number

}

// Current token is an opening brace, push it to 'ops' stack

else if (tokens[i] == '(')

ops.push(tokens[i]);

// If Closing brace encountered, solve the entire brace

else if (tokens[i] == ')') {

while (ops.peek() != '(')

values.push(applyOp(ops.pop(), values.pop(), values.pop()));

ops.pop();

}

// Current token is an operator.

else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/') {

// While top of 'ops' has same or greater precedence to current

// token, which is an operator. Apply operator on top of 'ops'

// to top two elements in values stack

while (!ops.empty() && hasPrecedence(tokens[i], ops.peek()))

values.push(applyOp(ops.pop(), values.pop(), values.pop()));

// Push current token to 'ops'.

ops.push(tokens[i]);

}

}

// Entire expression has been parsed at this point, apply remaining ops to remaining values

while (!ops.empty())

values.push(applyOp(ops.pop(), values.pop(), values.pop()));

// Top of 'values' contains result, return it

return values.pop();

}

// Returns true if 'op2' has higher or same precedence as 'op1',

// otherwise returns false.

public static boolean hasPrecedence(char op1, char op2) {

if (op2 == '(' || op2 == ')')

return false;

if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))

return false;

else

return true;

}

// A utility method to apply an operator 'op' on operands 'a' and 'b'. Return the result.

public static int applyOp(char op, int b, int a) {

switch (op) {

case '+':

return a + b;

case '-':

return a - b;

case '*':

return a * b;

case '/':

if (b == 0)

throw new UnsupportedOperationException("Cannot divide by zero"); // Handle exceptions

return a / b;

}

return 0;

}

// Driver method to test above methods

public static void main(String[] args) {

System.out.println(ArithmeticExpressionEvaluator.evaluateArithmeticExpression("( 1 + 2 ) * ( 6 / 2 + 3 )"));

}

}

The output is = 18.

Please let me know in case of any clarifications required. Thanks!