You are given the following Java files:
You are given the following Java file: TestCharacterStack.java inside the class add the method below:
public boolean ParenthesesMatching (String text)
takes a string text, and parenthesis whether its parenthesis are "balanced."
Hint: for left parenthesis, push onto stack; for right parenthesis, pop from stack and check
whether popped element matches right parenthesis, else return false.
If returned value is true, print “no delimiter error.”, else print “delimiter error”.
-Suppose the entered text (a+b(1*c-2)*1)
When call the method, the output will be: no delimiter error.
-Suppose the entered text ((a+b*(c+a)+5)
When call the method, the output will be: delimiter error.
import java.util.Stack;
public class TestCharacterStack {
public static void main(String[] args) {
TestCharacterStack t = new
TestCharacterStack();
if(t.ParenthesesMatching("(a+b(1*c-2)*1)"))
System.out.println("no delimiter error.");
else
System.out.println("delimiter error.");
if(t.ParenthesesMatching("((a+b*(c+a)+5)"))
System.out.println("no delimiter error.");
else
System.out.println("delimiter error.");
}
public boolean ParenthesesMatching(String text)
{
Stack<Character> stack = new
Stack<Character>();
boolean flag = true;
char c;
for (int i = 0; i <
text.length() && flag; i++) {
c =
text.charAt(i);
if (c == '(')
{
stack.push(c);
continue;
}
switch (c)
{
case ')':
if (stack.pop() != '(')
flag = false;
break;
}
}
return (stack.isEmpty() &&
flag);
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME
Get Answers For Free
Most questions answered within 1 hours.