Using an appropriate data structure write a program that mak
Using an appropriate data structure write a program that makes sure a JAVA program’s grouping symbols match correctly. These are symbols like parentheses( ), curly braces { }, Angle brackets < >, and Brackets [ ].
For example, your program should print an error message (about an incorrect match) if your java source file contains the following class definition:
}}
Solution
import org.dataorg.linear.JStack;
public class BalancedParenthisisCheck {
public static String checkBalance(String str) {
JStack stack = new JStack();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == \'[\' || ch == \'(\' || ch == \'{\') {
stack.push(ch);
} else if ((ch == \']\' || ch == \'}\' || ch == \')\')
&& (!stack.isEmpty())) {
if (((char) stack.peek() == \'(\' && ch == \')\')
|| ((char) stack.peek() == \'{\' && ch == \'}\')
|| ((char) stack.peek() == \'[\' && ch == \']\')) {
stack.pop();
} else {
return \"Not Balanced\";
}
} else {
if ((ch == \']\' || ch == \'}\' || ch == \')\')) {
return \"Not Balanced\";
}
}
}
if (stack.isEmpty())
return \"Balanced Parenthisis\";
else
return \"Not Balanced\";
}
public static void main(String[] args) {
String str = \"([{}])\";
if (str.isEmpty) {
System.out.println(\"Empty String\");
return;
}
System.out.println(BalancedParenthisisCheck.checkBalance(str));
}
}
public class JStack {
private Object[] data;
private int index = 0;
private int size = 0;
public JStack() {
super();
this.size = 30;
data = new Object[this.size];
}
public void push(T o) {
if (index >= size) {
this.increaseSize();
}
this.data[index] = o;
index++;
}
public Object pop() {
if (index != 0) {
Object obj = data[index - 1];
this.data[index - 1] = null; // Deleted
index--;
return obj;
} else
return null;
}
public Object peek() throws RuntimeException {
if (index != 0)
return this.data[index - 1];
else
return null;
}
public boolean isEmpty() {
return index == 0 ? true : false;
}
public int getStackSize() {
return index;
}
public void deleteStack() {
if (index != 0) {
for (int i = 0; i < index; i++) {
this.data[i] = null;
}
index = 0;
System.out.println(\"The Stack has been deleted\");
}
}
private void increaseSize() {
Object[] temp = new Object[size];
size = size * 2;
for (int i = 0; i < index; i++) {
temp[i] = this.data[i];
}
this.data = new Object[this.size];
for (int i = 0; i < index; i++) {
this.data[i] = temp[i];
}
}
}
import org.dataorg.linear.JStack;
public class BalancedParenthisisCheck {
public static String checkBalance(String str) {
JStack stack = new JStack();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == \'[\' || ch == \'(\' || ch == \'{\') {
stack.push(ch);
} else if ((ch == \']\' || ch == \'}\' || ch == \')\')
&& (!stack.isEmpty())) {
if (((char) stack.peek() == \'(\' && ch == \')\')
|| ((char) stack.peek() == \'{\' && ch == \'}\')
|| ((char) stack.peek() == \'[\' && ch == \']\')) {
stack.pop();
} else {
return \"Not Balanced\";
}
} else {
if ((ch == \']\' || ch == \'}\' || ch == \')\')) {
return \"Not Balanced\";
}
}
}
if (stack.isEmpty())
return \"Balanced Parenthisis\";
else
return \"Not Balanced\";
}
public static void main(String[] args) {
String str = \"([{}])\";
if (str.isEmpty) {
System.out.println(\"Empty String\");
return;
}
System.out.println(BalancedParenthisisCheck.checkBalance(str));
}
}
public class JStack {
private Object[] data;
private int index = 0;
private int size = 0;
public JStack() {
super();
this.size = 30;
data = new Object[this.size];
}
public void push(T o) {
if (index >= size) {
this.increaseSize();
}
this.data[index] = o;
index++;
}
public Object pop() {
if (index != 0) {
Object obj = data[index - 1];
this.data[index - 1] = null; // Deleted
index--;
return obj;
} else
return null;
}
public Object peek() throws RuntimeException {
if (index != 0)
return this.data[index - 1];
else
return null;
}
public boolean isEmpty() {
return index == 0 ? true : false;
}
public int getStackSize() {
return index;
}
public void deleteStack() {
if (index != 0) {
for (int i = 0; i < index; i++) {
this.data[i] = null;
}
index = 0;
System.out.println(\"The Stack has been deleted\");
}
}
private void increaseSize() {
Object[] temp = new Object[size];
size = size * 2;
for (int i = 0; i < index; i++) {
temp[i] = this.data[i];
}
this.data = new Object[this.size];
for (int i = 0; i < index; i++) {
this.data[i] = temp[i];
}
}
}





