Question

JAVA - Without using build in functions Implement a program that will use a stack structure...

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 reporting' (and report any     possible errors outside the main scanner loop).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please check the following code:- The code prints the output as They are Balanced if there is the correct placement of parenthesis and prints the output as They are UnBalanced if there the placement of parenthesis are not correct.

// Java program for checking

// balanced Parenthesis

public class BalancedParan

{

                static class stack

                {

                                int top=-1;

                                char items[] = new char[100];

                                void push(char x)

                                {

                                                if (top == 99)

                                                {

                                                                System.out.println("Stack full");

                                                }

                                                else

                                                {

                                                                items[++top] = x;

                                                }

                                }

                                char pop()

                                {

                                                if (top == -1)

                                                {

                                                                System.out.println("Underflow error");

                                                                return '\0';

                                                }

                                                else

                                                {

                                                                char element = items[top];

                                                                top--;

                                                                return element;

                                                }

                                }

                                boolean isEmpty()

                                {

                                                return (top == -1) ? true : false;

                                }

                }

               

                /* Returns true if character1 and character2

                are matching left and right Parenthesis */

                static boolean isMatchingPair(char character1, char character2)

                {

                if (character1 == '(' && character2 == ')')

                                return true;

                else if (character1 == '{' && character2 == '}')

                                return true;

                else if (character1 == '[' && character2 == ']')

                                return true;

                else

                                return false;

                }

               

                /* Return true if expression has balanced

                Parenthesis */

                static boolean areParenthesisBalanced(char exp[])

                {

                /* Declare an empty character stack */

                stack st=new stack();

               

                /* Traverse the given expression to

                                check matching parenthesis */

                for(int i=0;i<exp.length;i++)

                {

                                               

                                /*If the exp[i] is a starting

                                                parenthesis then push it*/

                                if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')

                                                st.push(exp[i]);

               

                                /* If exp[i] is an ending parenthesis

                                                then pop from stack and check if the

                                                popped parenthesis is a matching pair*/

                                if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')

                                {

                                                               

                                                /* If we see an ending parenthesis without

                                                                a pair then return false*/

                                                if (st.isEmpty())

                                                {

                                                                return false;

                                                }

               

                                                /* Pop the top element from stack, if

                                                                it is not a pair parenthesis of character

                                                                then there is a mismatch. This happens for

                                                                expressions like {(}) */

                                                else if ( !isMatchingPair(st.pop(), exp[i]) )

                                                {

                                                                return false;

                                                }

                                }

                                               

                }

                               

               

                               

                if (st.isEmpty())

                                return true; /*balanced*/

                else

                                { /*not balanced*/

                                                return false;

                                }

                }

               

                /* UTILITY FUNCTIONS */

                /*driver program to test above functions*/

                public static void main(String[] args)

                {

                                char exp[] = {'{','(',')','}','[',']'};

                                if (areParenthesisBalanced(exp))

                                                System.out.println("They are Balanced ");

                                else

                                                System.out.println("They are UnBalanced ");

                }

}

Add a comment
Know the answer?
Add Answer to:
JAVA - Without using build in functions Implement a program that will use a stack structure...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a program that will use a stack structure to check for correct placement of parentheses...

    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...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • Using Java In this project, you are going to build a max-heap. You will use an...

    Using Java In this project, you are going to build a max-heap. You will use an array to implement the heap. Your program should: ? Allow the user to select one of the following two choices (Note that your program needs to implement both choices): o (1) test your program with 100 randomly generated integers (no duplicates, positive numbers with proper range); o (2) test your program with the following 100 fixed values from 1, 2, 3, ..., and 100....

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

    C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....

  • Please use the JAVA code attached as an input to the program that must be created...

    Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instructions of the program: java code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * */ import java.util.Random; public class Rand_Z3_Exp { /** * @param args the command line arguments */ public static void main(String[] args) {...

  • Assignment4: Evaluate Arithmetic Expressions. Requirements: Implement a concrete ArrayStack class that extends the IStack interface as...

    Assignment4: Evaluate Arithmetic Expressions. Requirements: Implement a concrete ArrayStack class that extends the IStack interface as we discussed in the class (any other different Stack class implementation, even if it is implemented by yourself, will not receive any credit). Write a test class called Evaluate and a method that evaluates an arithmatic expression, which is given by a string. import java.util.Scanner; public class Evaluate { public static void main(String[] args) } // your implementation // obtain user's input from keyboard...

  • Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instru...

    Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instructions of the program: java code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * */ import java.util.Random; public class Rand_Z3_Exp { /** * @param args the command line arguments */ public static void main(String[] args) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT