An infix expression is valid if:

  • it consists of at least one operand or at least one operator and two operands
  • it contains only integer operands ranging from 0 to 99,999
  • it contains only the +, -, *, /, and % operators, all of which are binary operators that perform integer-based arithmetic in accordance with the usual Java precedence rules
  • it does not require division by zero
  • it has whitespace (any nonempty combination of spaces, tabs, and newlines) between each adjacent pair of tokens
  • it is balanced with respect to parentheses, braces, brackets, and angle brackets
    • These refer to the following symbols, respectively: ( ) { } [ ] < >
    • All such symbols have equal precedence
    • All expressions within such balance symbols are also valid infix expressions

Your implementation of the interface should return a String in one of the following formats:

  • “expression is a valid infix expression with a value of value”, where expression is the input expression that evaluates to value
  • “expression is an invalid expression with reason”, where expression is the input expression and reason is an explanation as to why expression is invalid
  • Sample reasons might include “unbalanced parentheses”, “insufficient operands”, and others
  • When multiple reasons exist, you need to report only one such reason

Your program may not throw any exceptions; it should handle all types of valid and invalid input without crashing. This requires you as the programmer to anticipate what problems could arise from invalid input and to handle them appropriately

Examples responses:

  • “[(3+10)*(20/6–1)]/3 is a valid infix expression with a value of 8”
  • “/ * + 3 10 / 20 – 6 1 3 is an invalid expression with tokens improperly ordered”
  • “3 10 + 20 6 1 - / * 3 / is an invalid expression with tokens improperly ordered”
  • “[(3+ )*(20/6–1)]/3 is an invalid expression with insufficient operands”
  • “((3+10)*(20/6–1)]/3 is an invalid expression with mismatched parentheses”

Implement ExpressionInterface interface and provide a working ExpressionFactory class.

Given String, identify it as a valid infix expression or an invalid infix expression. Evaluate valid infix expressions without converting to an expression of another form (e.g. postfix, prefix, etc.). Report each invalid infix expression as invalid, explaining why it is invalid.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.