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:


Data Structures Prof. Vickram Sawh (due-date: 1 week) Part a) Implement the Stack class, and use...
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) 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 (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...