Construct and evaluate an arithmetic expression , as follows: Your expression mu
ID: 3736256 • Letter: C
Question
Construct and evaluate an arithmetic expression, as follows:
Your expression must contain at least 5 different operators, including at least one division and one modulo (remainder) operator.
Start with an expression that contains no parentheses.
Example: 5 * 4 % 3 + 10 - 32 / 10
Add parentheses that show how the expression would be evaluated, using the default operator precedence.
Example: ((((5 * 4) % 3) + 10) - (32 / 10))
Show each step that Java would take to evaluate the expression and give the final answer that the expression would evaluate to.
Example:
Step 1: Evaluate (5 * 4) to get ((20 % 3) + 10) - (32 / 10)
Step 2: Evaluate (20 % 3) to get (2 + 10) - (32 / 10)
Step 3: Evaluate (32 / 10) to get ((2 + 10) - 3)
Step 4: Evaluate (2 + 10) to get (12 - 3)
Step 5: Evaluate (12 - 3) to get 9
Explanation / Answer
Let us take the expression :4*5+22/11-4+6%3.
We know the operator precedence that java follows goes as:
highest to lowest priority (->): () ->++,--(post) ->++,--(pre) -> *,/,% -> +,-(left to right).
Using this order, we can simplify the expression.
The expression according to the default operator precedence will evolve to the following:
((4*5)+(22/11))-4+(6%3)
The above statement would then be executed as follow:
Step 1 : Evaluate (4*5) and we get the new expression (20+(22/11))-4+(6%3)
step 2: Evaluate (22/11), resulting in (20+2)-4+(6%3)
step 3: The expression simply becomes then : 20+2-4+0
Thus, the expression evaluates to, 18.