Question: private boolean isValid() This private method should return true if the infix expression is valid in all respects, and false otherwise. For example, isValid() should
private boolean isValid() This private method should return true if the infix expression is valid in all respects, and false otherwise. For example, isValid() should return false for the infix expression "( 3 ( ( * * 4 ) 8 ) 7 7 ) 6", even though the parentheses are balanced. isValid() should check all of the following:
Whether the expression contains illegal characters (the only valid characters are the digits 0 through 9, the 6 operators and parentheses symbols + - * / % ^ ( ), and spaces.
Whether the parentheses are valid (by calling on isBalanced())
Whether there is an improper order of operators and operands. Observe that each
operator must have an operand before it and after it (an operand can be a number, or an expression in parentheses). For example, the following are NOT valid infix expressions:
"*34"
"3 4 +"
"+"
"3 (4 + 5)" // note that this is implied multiplication...something we will consider
NOT valid
"3**4"
"410"
Note that these ARE valid infix expressions:
"3"
"3 * (5)"
"(4 + 9)"
"((4 + 9))"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
