Computers and Technology

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.

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 07:30, tinasidell1972
An endless cycle of creation and response on the internet is called
Answers: 1
image
Computers and Technology, 24.06.2019 00:30, louie8656
Match the sentence fragment in the first column with the appropriate ending in the second column. a little per favore?
Answers: 1
image
Computers and Technology, 24.06.2019 02:50, rurbanok12
Be sure to answer all parts. synthesize the following compound from benzene. a. b. c. d. e. f. reaction [1] conditions: a b c d e f reaction [1] product: draw structure reaction [2] conditions: a b c d e f reaction [2] product: draw structure reaction [3] conditions: a b c d e f reaction [3] product:
Answers: 3
image
Computers and Technology, 24.06.2019 17:40, orlandokojoasem1234
Write an assembly language program to input a string from the user. your program should do these two things: 1. count and display the number of words in the user input string. 2. flip the case of each character from upper to lower or lower to upper. for example if the user types in: "hello there. how are you? " your output should be: the number of words in the input string is: 5 the output string is : hello there. how are you?
Answers: 2
Do you know the correct answer?
A classic use of a stack if for checking whether an expression that contains a set of different kind...

Questions in other subjects:

Konu
Mathematics, 09.01.2020 11:31