Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Consider the problem description below:
John proposed an algorithm balancedBracketsByCounting(String s), as follows, that takes a string
as an input and checks whether the brackets \[" and \]" in the string are matched correctly. He
claimed that his algorithm is correct. For example, balancedBracketsByCounting("[x][[xy]]")
would return true but balancedBracketsByCounting("[[x[y]]") would return false.

This is John's algorithm:
```boolean balancedBracesByCounting(String s) {
numOfLeftBracketRound = 0; numOfRightBracketRound = 0
numOfLeftBracketSquare = 0; numOfRightBracketSquare = 0
balanced = true
while (not at the end of the s) {
if (the next character is an open bracket '(' {
numOfLeftBracketRound++
}
else if (the next character is an open bracket '[') {
numOfLeftBracketSquare++
else if (the next character is a close bracket ')' {
numOfRightBracketRound- -
} else if (the next character is a close bracket ']') {
numOfRightBracketSquare- -
}
}
return {(noOfLeftBracketRound==noOfRightBracketRound)
and (noOfLeftBracketSquare==noOfRightBracketSquare)}
}```
Study carefully John's algorithm and
1. Explain why the algorithm balancedBracketsByCounting(String s) is
awed.
2. Demonstrate one
awed case in which, with the aid of an example of input, the algorithm may
return a true for a wrong match.
3. Identify as many errors or weaknesses as possible in John's algorithm, and correct or improve
them. Summarise your results in a table
4. Derive an alternative algorithm that would return correct answers. Show all your work.
5. Implement your algorithm, referring to the submission requirements.

2. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

And what exactly is the problem and/or question? I hope you aren't relying on us to answer the bottom 5 points for you, as this is your job. If you have a specific question about the work, by all means ask and we can try to help

3. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

No, i am not. I am new to java and this algorithm thing. I just need someone to answer the first and second questions for me with some details why the algorithm is flawed. i need to understand what is wrong with it. Rightnow i don't understand it.

4. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

You've pretty much just described your homework assignment. That's not how this works. What do you think is the answer? What have you done to try to figure it out? Have you tried running the code and trying it yourself?

Also, posts with titles like yours tend to be ignored. Everybody here needs help. Yelling "please" won't help you- in fact, it will hurt your chances of getting help.

5. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

Thanks. I got it done already.

6. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

Hi, I'm doing the same assignment and is getting the same problem with understanding it especially with question 2 as I am not sure what is needed here. Any pointers on this? It'll be much appreciated. Thanks!

7. ## Re: URGENT HELP NEEDED WITH ALGORITHM PLEASE! PLEASE! .... THANKS

Originally Posted by copeg
And what exactly is the problem and/or question? I hope you aren't relying on us to answer the bottom 5 points for you, as this is your job. If you have a specific question about the work, by all means ask and we can try to help
Hi I have the same assignment as well and I'm getting a problem with question 2, any help/pointers on this will be much appreciated, thanks!