Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 image text in transcribed image text in transcribed image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

=+ (b) Define a set function v, on , by (11.6) "o (f, g] = A(g-f).

Answered: 1 week ago

Question

The Nature of Nonverbal Communication

Answered: 1 week ago

Question

Functions of Nonverbal Communication

Answered: 1 week ago

Question

Nonverbal Communication Codes

Answered: 1 week ago