/ CSIT 212 Data Structures and Algorithn Stack Application Assignnent This Java
ID: 3903321 • Letter: #
Question
/ CSIT 212 Data Structures and Algorithn Stack Application Assignnent This Java Program checks for batanced parentheses in an expression using stack. ./ /* This interactive progra allows a user to enter various parenthesized expressions e which are then checked for correctly matched and balanced parentheses Author: H. A. Johnson BatancedParentheses. java package nyprest mport AyproiectStask mport java.utit.scanner: public class BalancedParentheses ( Stack s-new Stack):l String para0penedI1(" String paraclosedti"] boolean pairkith(String x) ( if (s.top().equals(")&x.equals;("1") } else if (s.top( ).equals("(") && ×.equals(")")) { } else if (s.top ( ) .equals("(") “ x.equals(.)")) { return false; return true; return true: return true boolean tscounterpart(string x) for (inti 0:i
Explanation / Answer
Part a)
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the string : ");
String x = s.nextLine();
System.out.println(x);
String array[] = new String[x.length()];
for(int i=0; i<x.length(); i++) {
array[i] = x.substring(i, i+1);
}
BalancedParentheses b = new BalancedParentheses();
boolean value = b.checkBalancedParentheses(array);
if(value) {
System.out.println("BalancedParentheses");
} else {
System.out.println("UnBalancedParentheses");
}
s.close();
}
Part b)
public static void main(String args[]) {
Stack s = new Stack();
boolean empty = s.isEmpty();
if(empty) {
System.out.println("stack is empty");
} else {
System.out.println("stack is not empty");
}
for(int i=0; i<s.MAXSIZE; i++) {
s.push(i*i);
}
System.out.println("Stack top = " + s.top());
System.out.println("The stack is : ");
s.display();
// now popping the top 3 elements
s.pop();
s.pop();
s.pop();
System.out.println("Now the stack is : ");
s.display();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.