Question: 3 parts questions Write a method which given a string containing an expression, checks to see if the parenthesis in the expression are balanced. For

3 parts questions 3 parts questions Write a method which given a string containing an expression, checks to see if the parenthesis in the expression are balanced. For example, the string "(3+5x*getInfo(thing.foop()))" the method will return true, but the

Write a method which given a string containing an expression, checks to see if the parenthesis in the expression are balanced. For example, the string "(3+5x*getInfo(thing.foop()))" the method will return true, but the below will all be false "x*(2))" "3+5x*getInfo(thing.foop()))" "(3+5x*getInfo(thing.foop())" "(3+5x*getInfo(thing.foop)))" We will work on this problem in 3 parts: First, what is the correct method declaration? O public static boolean is Balanced(Stack expression) { O public static void is Balanced(String expression) { O public static boolean is Balanced(String expression) { o public static boolean isBalanced(String expression) { A To start our method off, we'll create a Stack to store the open parenthesis we encounter. We will then read every character in the expression. If we encounter a '(' we push it onto the stack. But, if we encounter a'), we will pop the stack and see if we have a '/' to match. If they don't balance out, we return false. If we get to the end and have an empty stack, we can return true. Let's first work on the for loop, and worry about what goes inside it for the next problem [Choose] [Choose] }//end for return stack.isEmpty(); // body of for loop left for later Stack stack = new Stack(); for (Character c: expression.toCharArray()) { [Choose] [Choose] 3 We will then read every character in the expression. If we encounter a '('we push it onto the stack. But, if we encounter a')', we will pop the stack and see if we have a '(' to match. If they don't balance out, we return false. Let's complete the inside of the for loop. Remember, c is the current character. [Choose ] [Choose] else if( c==')') { if(c == ''){ stack.push('(');} } // end else if if(stack.isEmpty(){return false; } if(!(opener=='/' && c==')')) { return false;} char opener = stack.pop(); [Choose] [Choose]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!