Question

Data Structures Prof. Vickram Sawh (due-date: 1 week) Part a) Implement the Stack class, and use it to test an algebraic expression. Create the following GUI: -textfield frame Check button The program checks to see if the expression typed in the textfield is balanced, using the [, and < symbols. The check occurs if the user clicks the button, and the result of the check is reported in a JOptionPane window. The JOptionPane window should state: The expression is balanced, or The expression is unbalanced at character .. (The ellipses indicates an integer indicating at which character position the expression was found to be unbalanced) Part b) Create a graph, showing the relative speed of sorting algorithms 1. Bubblesort

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

Hi,

Please find below class StackExp.java. I have created simple GUI with a text box and a button. On clicking on button the expression entered in textbox is checked if it is balanced or not. After validating, appropriate message is shown in Dialog box using JOptionPane. I have used Stack class to push and pop characters to check for balancing. Please find below output screenshots. Let me know if you have any questions.

StackExp.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class StackExp implements ActionListener{

//Create GUI using TextBox,Frmame and a button
        JFrame f;
        JTextField tf ;
        JButton b;  

        public StackExp(){
                f=new JFrame("Balance Expression");
                tf = new JTextField(200);
                b=new JButton("Check");  
                b.addActionListener(this);
                tf.setBounds(50,50,100,20);
                b.setBounds(50,100,95,30);
                f.add(tf);
                f.add(b);
                f.setSize(400,400);  
                f.setLayout(null);  
                f.setVisible(true);

        }
        public static void main(String[] args) {
                //Create frame
                StackExp frame = new StackExp();

        }

        //On clicking check button this method is invoked 
        @Override
        public void actionPerformed(ActionEvent e) {
                //Take input expression from textbox
                String exp = tf.getText();
                //boolean to check if the expression is balanced
                boolean isBalanced = true;
                Stack<Character> stack  = new Stack<Character>();//Create object of Stack
                //If expression length is odd, then it is not balanced
                if ((exp.length() % 2) == 1) 
                        isBalanced = false;

                else{
                        for(int i = 0; i < exp.length(); i++) {
                                char c = exp.charAt(i);
                                //If it is starting bracket then push it to stack
                                if(c == '[' || c == '<' || c == '{' ) {     
                                        stack.push(c);
                                } 
                //Else check if the ending bracket has corresponding starting bracket by
                                //popping from stack. If not, then expression is not balanced
                                else if(c == ']') {
                                        if(stack.isEmpty() || stack.pop() != '[') {
                                                isBalanced = false;
                                                break;
                                        }
                                } else if(c == '>') {
                                        if(stack.isEmpty() || stack.pop() != '<') {
                                                isBalanced = false;
                                                break;
                                        }           
                                } else if(c == '}') {
                                        if(stack.isEmpty() || stack.pop() != '{') {
                                                isBalanced = false;
                                                break;
                                        }

                                }
                        }
                }
                //Display Message Dialog using JOptionPane
                if(isBalanced && stack.isEmpty())
                        JOptionPane.showMessageDialog(f,"The Expression is balanced.");  
                else
                        JOptionPane.showMessageDialog(f,"The Expression is not balanced.");
        }
}


Output Screenshots:

Check Message The Expression is balanced. OK

Balance Expression 11개 Check Message The Expression is not balanced. OK

Add a comment
Know the answer?
Add Answer to:
Data Structures Prof. Vickram Sawh (due-date: 1 week) Part a) Implement the Stack class, and use...
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
  • 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...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

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