Question

You are given the following Java files: TestCharacterStack.java that contains class LLStack with class stackNode listed...

You are given the following Java files:

  • TestCharacterStack.java that contains class LLStack with class stackNode listed as inner class.

  

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
I am having some trouble writing some java code involving strings. I have included the code...
I am having some trouble writing some java code involving strings. I have included the code provided by my professor below. that has the instructions in it as well public class StringExercise { public static void main(String[] args) { String given = "The quick brown fox jumped over the moon!"; String given2 = "mary"; String given3 = "D"; //Write a Java code snippet to check (print a boolean) whether the given String ends with the contents of "oon!", see endsWith...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
You are given the following Java files: EvaluateExprission that you will use to add a method....
You are given the following Java files: EvaluateExprission that you will use to add a method. In EvaluateExprission.java write a recursive method    public static int EvaluateE (int x, int y) The method should evaluate the expression bellow and take the values x, y from the user. Update the main to call EvaluateE method properly . E = xy +x* (x-1)(y-1) +x* (x-2)(y-2) + . . . +1
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...