One application of stacks is to keep track of things that must match up such as parentheses in an expression or braces in a program. In the case of parentheses when a left parenthesis is encountered it is pushed on the stack and when a right parenthesis is encountered its matching left parenthesis is popped from the stack. If the stack has no left parenthesis, that means the parentheses don’t match—there is an extra right parenthesis. If the expression ends with at least one left parenthesis still on the stack then again the parentheses don’t match—there is an extra left parenthesis.
File ParenMatch.java contains the skeleton of a program to match parentheses in an expression. It uses the Stack class provided by Java (in java.util). Complete the program by adding a loop to process the line entered to see if it contains matching parentheses. Just ignore characters that are neither left nor right parentheses. Your loop should stop as soon as it detects an error. After the loop print a message indicating what happened — the parentheses match, there are too many left parentheses, or there are too many right parentheses. Also print the part of the string up to where the error was detected.
// ********************************************************************
// ParenMatch.java
//
// Determines whether or not a string of characters contains
// matching left and right parentheses.
// ********************************************************************
import java.util.*;
import java.util.Scanner;
public class ParenMatch
{
public static void main (String[] args)
{
Stack<Character> s = new Stack<Character>();
String line; // the string of characters to be checked
Scanner scan = new Scanner(System.in);
System.out.println ("\nParenthesis Matching");
System.out.print ("Enter a parenthesized expression: ");
line = scan.nextLine();
// loop to process the line one character at a time
// print the results
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// ParenMatch.java
import java.util.*;
import java.util.Scanner;
public class ParenMatch
{
public static void main(String[] args)
{
Stack<Character> s = new Stack<Character>();
String line; // the string of characters to be checked
Scanner scan = new Scanner(System.in);
System.out.println("\nParenthesis Matching");
System.out.print("Enter a parenthesized expression: ");
line = scan.nextLine();
// denoting if there is an error in the expression
boolean error = false;
// string to store the string upto where the error was detected, if any
String str = "";
// loop to process the line one character at a time
for (int i = 0; i < line.length(); i++) {
// appending current character to str
str += line.charAt(i);
// checking if char is '('
if (line.charAt(i) == '(') {
// pushing to stack
s.push('(');
}
// otherwise checking if it is ')'
else if (line.charAt(i) == ')') {
// if the stack is empty, there is no opening parentheses found
// for the current closing parentheses, means too many right
// parentheses
if (s.isEmpty()) {
System.out.println("Too many right parentheses");
error = true; // error detected
break; // end of loop
} else {
// removing top '(' from stack
s.pop();
}
}
}
// at the end if stack is not empty, it means that there are too many
// left parentheses (that is left to be closed)
if (!error && !s.isEmpty()) {
System.out.println("Too many left parentheses");
error = true;
}
// if no error is detected, displaying 'parentheses match'
if (!error) {
System.out.println("parentheses match");
} else {
// otherwise displaying the string upto which the error is detected
System.out.println("String upto where the error was detected: "
+ str);
}
}
}
/*OUTPUT (different runs)*/
Parenthesis Matching
Enter a parenthesized expression: ((1+2)+(1/2))
parentheses match
Parenthesis Matching
Enter a parenthesized expression: ((1+2)+(1/2
Too many left parentheses
String upto where the error was detected: ((1+2)+(1/2
Parenthesis Matching
Enter a parenthesized expression: 1+2)+(1/2))
Too many right parentheses
String upto where the error was detected: 1+2)
One application of stacks is to keep track of things that must match up such as...
I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...
Implement a program that will use a stack structure to check for correct placement of parentheses in an algebraic expression. Allow the use of ( ) [ ] { } characters as grouping symbols. Make sure that an error is reported for an expression of a form (...]. In addition report other possible parentheses related errors (too many levels, too many right paren., too many left paren.). Make sure to use 'silent error reporting' (and report any possible errors outside...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
Stacks are used by compilers to help in the process of
evaluating expressions and generating machine language code.In this
exercise, we investigate how compilers evaluate arithmetic
expressions consisting only of constants, operators and
parentheses. Humans generally write expressions like 3 + 4and 7 /
9in which the operator (+ or / here) is written between its
operands—this is called infix notation. Computers “prefer” postfix
notation in which the operator is written to the right of its two
operands. The preceding...
JAVA - Without using build in functions Implement a program that will use a stack structure to check for correct placement of parentheses in an algebraic expression. Allow the use of ( ) [ ] { } characters as grouping symbols. Make sure that an error is reported for an expression of a form (...]. In addition report other possible parentheses related errors (too many levels, too many right paren., too many left paren.). Make sure to use 'silent error...
Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...
You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2) One alphabetic character - "x" (representing a value to be supplied later). (3) Binary operators (+, -, *, / and % (modulo)). (4) Parentheses You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...
For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...
Please Code in Java and follow the directions given
Thanks
Program required
Directions
.A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...