Balanced Parens

Write a test class named BalancedParens that contains a static method named balancedParens that returns a boolean value to indicate whether or not the parentheses and square brackets in a string are balanced or not. The string will represent a Java mathematical expression (e.g. (x + y) * Math.pow(2, 4) + name[3] ). The function returns true if there is a matching number of left parentheses and right parentheses as well as matching numbers of right and left square brackets and right and left curly braces and if these symbols are properly nested. For example, if there is one right square bracket and no matching left square bracket inside of a set of parentheses, the function should return false. Or, if there are 3 right curly braces and only 2 left curly braces, the function must return false. You must use one or more Stack and/or Queue's in this program. But the method must be as efficient as possible. The test class must read the inputted mathematical expressions as String objects using Scanner. It must allow the user to input many separate expressions and it must display the word true or false for each inputted expression. The program must end when the user inputs the word exit.

Your balancedParens method must return a false for the following test cases (among others):

  • (2 + ] + 3
  • (Math.po[3,)]w
  • )2 + 3(
  • {[(2 + 3)]
  • ((2 + 3)))
  • {(2 + 3) + (2 + 3)]

Your balancedParens method must return a true for the following test cases (among others):

  • (2 + 3)
  • [(2 + 3) + (x - y)]
  • 3(4 + x)
  • Math.pow(3, 4)

and even

  • (2 + ) 3
  • Math.po(3,)w4
  • (23+)

since technically the parentheses are balanced. The method does not have to test the syntactical or logical validity of an expression other than having balanced parentheses, brackets & curly braces.

Your program must follow the class Coding Standards.

Preconditions:

  • The inputted string will contain 1 or more characters.

You must hand in the following on separate pages stapled in this specified order:

  1. The source code for the BalancedParens class.
  2. The printscreen of your console window output with at least 6 test cases some with balanced parens and some without. Be sure to print black text on white background.