A stack can be used to check whether an arithmetic expression such as

3*(4+(5-6))/7

has properly balanced parentheses. Break the expression into its individual parts.

3 * ( 4 + ( 5 - 6 ) ) / 7

Push each part on a stack as it is encountered. When a ) is encountered, pop everything until the matching (. If the stack is prematurely empty, then report an error.

At the end, there should be no parentheses left. Otherwise, report an error.

Complete the following file:

ExpressionChecker.java

import java.util.Stack; public class ExpressionChecker { public boolean checkParentheses(String expression) { Stack<String> stk = new Stack<String>(); for (int i = 0; i < expression.length(); i++) { String part = expression.substring(i, i + 1); stk.push(part); // TODO: Complete this loop } while (!stk.isEmpty()) { String part = stk.pop(); // TODO: Complete this loop } // TODO: Complete this statement return ...; } }