Question

In: Computer Science

in Java language, in most simple algorithm Using a stack class, write a static method called...

in Java language, in most simple algorithm

  1. Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example

parse(“{3 + {4/2} }”)   would return -1

parse(“{ { 4*X}”) would return 6 since at position 6 we expected another “}”

parse(“{3+4}}”) would also return a 6

Solutions

Expert Solution

The program is written as per the requirements. The comments are provided in the code for better understanding of how the logic works.

import java.util.*;

class Test{
   public static void main(String[] args) {
       int result = parse("{3+4}}");
       System.out.println(result);
   }
  
   static int parse(String expression) {
       //Declare a stack
       Stack<String> expressionStack = new Stack<String>();
      
       //Add each characters of the expression variable to the stack.
       for(int i = 0; i < expression.length(); i++) {
           expressionStack.push(Character.toString(expression.charAt(i)));
       }
      
       //These variables are used for counting the left and right braces.
       int leftBracesCount = 0;
       int rightBracesCount = 0;
      
       //Initializing the variable i to the stack size. This is decreased by 1 inside the loop in each iteration to find out the bad position of the braces.
       int i = expressionStack.size();
       int result = -1;
       //Get each character from the stack inside the loop.
       while(!expressionStack.empty()) {
           String characterExtracted = expressionStack.pop();
           switch(characterExtracted) {
               case "{":
                   leftBracesCount++;
                   if(result == -1)
                       result = i;
                   break;
               case "}":
                   rightBracesCount++;
                   if(result == -1)
                       result = i;
                   break;
           }

           i--;
       }
      
       if(leftBracesCount == rightBracesCount)
           result = -1;

       return result;
   }
}


Related Solutions

In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT