Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Application of stack
- 1. Complete the implementation of MyStack class from Chapter 6.
- 2. Write a class StackApplication that includes the following methods:
- 3. Implement the method balanced in the StackApplication class:
- public static boolean balanced(String str)
- // Parameter: str contains characters, which may contain
- // '(' or ')'.
- // Return: The method returns true if the String str form
- // a sequence of correctly balanced parentheses with each
- // '(' and matching a ')'. Note that a sequence such as
- // ) ( is NOT balanced.
- The algorithm to check for balanced parentheses:
- 1. create an empty stack.
- 2. for every character from str
- 2.1 if it is '(' push it on the stack
- 2.2 if it is ')'
- 2.2.1 if stack is not empty
- 2.2.1.1 pop one item from the stack
- else
- 2.2.1.2 return false
- 3 if stack is empty
- 3.1 return true
- else
- 3.2 return false
- 4. Implement a main method in the StackApplication class that will test the balanced method:
- Example:
- Input str: (a + b)
- (a + b) is balanced
- Input str: (a + b
- (a + b) is not balanced
- Input str: a + b)
- a + b) is not balanced
- Input str: a + (b)
- a + b) is balanced
- Input str: ((a + (b))
- ((a + (b)) is not balanced
- Input str: ((a) + (b))
- ((a )+ (b)) is balanced
- Input str: a) + (b
- a) + (b is not balanced
- 5. Use your own expressions to test the balanced method.
- 6. Submit a complete report for this assignment using the Assignment Guide.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement