subject

A classic use of a stack if for checking whether an expression that contains a set of different kinds of bracketing (namely parentheses, square brackets, curly brackets, angle brackets) is "well formed". Such an expression is considered "well formed" if the open brackets match exactly the close brackets. This problem represents one portion of the design of a compiler that needs to check that all such pairs of brackets are properly balanced and nested. You will write a method that checks a given expression (a String) with characters and brackets ("()", "[]", "{}", "<>"), determine if it is well-formed by using the following algorithm in conjunction with a stack. • Check the expression character by character. If the character is an opening bracket (characters ‘(‘, ‘[‘, ‘{‘, ‘<‘), then push it onto the stack • Else if the character is a closing bracket (characters ‘)’, ‘]’, ‘}’, ‘>’), then: • Check if the stack is empty. If it is, then there is no matching opening bracket. The expression is not well formed. • If the stack is not empty, pop the top character off the stack, compare the current closing bracket to the top character. If they are matched brackets, continue to read next character in the expression; if they are not matched brackets, the expression is not well formed. • If the end of the expression is reached, check if the stack is empty. If it is, the expression is well-formed. Otherwise, it is not well-formed.
Activities 1. Start Eclipse and create a new Java package lab2, create a new class named BalanceCheck. 2. Write a static method named matchCheck(String s). It should return true if the parameter string s is well-formed; return false otherwise. (1) Your code must instantiate a Stack to contain Character class objects using the java. util. Stack class. (2) Implement the algorithm in the introduction. (3) Your code should print meaningful error message when the expression is not well-formed. For example, "missing open bracket" if current character is a close bracket but stack is empty, "missing close bracket" if the end of string is reached but stack is not empty, or "mismatch brackets". 3. Write the main method in the same class, declare some strings and call the matchCheck(String s) method to check if your method works fine.

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 06:30
Which option correctly describes a dbms application? a. software used to manage databases b. software used to organize files and folders c. software used to develop specialized images d. software used to create effective presentations
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
What is the total resistance in a circuit that contains three 60 ohm resistors connected in a series? a. 20 ohms b. 120 ohms c. 60 ohms d. 180 ohms
Answers: 2
question
Computers and Technology, 23.06.2019 21:30
Enzo’s balance sheet for the month of july is shown. enzo’s balance sheet (july 2013) assets liabilities cash $600 credit card $4,000 investments $500 student loan $2,500 house $120,000 mortgage $80,000 car $6,000 car loan $2,000 total $127,100 total $88,500 which expression finds enzo’s net worth?
Answers: 1
question
Computers and Technology, 24.06.2019 14:00
In the microsoft® access® and microsoft excel® programs, the ribbon contains tabs that are divided into with like tools in them. parts groups containers bunches
Answers: 1
You know the right answer?
A classic use of a stack if for checking whether an expression that contains a set of different kind...
Questions
question
Mathematics, 25.04.2021 02:30
question
English, 25.04.2021 02:30
Questions on the website: 13722361