YOU MUST INSTANTIATE AN OBJECT OF YOUR OWN CLASS NO MORE USI
YOU MUST INSTANTIATE AN OBJECT OF YOUR OWN CLASS. NO MORE USING \"public static\" methods
Write a program to read in a text file (of your own making) and test for balanced parenthesis and balanced brackets. Call the file: “assn3data.txt”. You can use Notepad (Win) or Textedit (Mac) to create the file, to make it easier on yourself – do not add any blank lines to the file.
Example of balanced parenthesis and brackets: ((x-y) + [n-1] / z )
Example of unbalanced parenthesis and brackets: ( a + b [ z + 1 ] ( z / 2)
The file can contain multiple lines, with each line having parenthesis and/or brackets. Each line will be self-contained, so you do not need to worry about matching parenthesis and brackets across multiple lines in the file. If a line does not contain any () or [] characters at all, consider it balanced.
You should use a stack data structure to test each line of the file for the balanced/unbalanced result.
Note: this program does not need to be recursive, but you can use recursion if you like.
Use a Queue data structure to hold and print out the results of the testing after all the lines in the file are processed. The Queue should hold the line number for each line of the file which was read in and the result (balanced or unbalanced) for each line number in the file. See example output below.
Your program should use a class, and have an object of that class which you instantiate and use. You should call methods to do most of the work in the program. Also, comment your code where appropriate. Every line does not need to be commented, but you should have a proper header, and each method should have some comments as well as a major section such as a loop, conditional statement or an abstract data type.
Make your output look like this example output:
File read in: <filename>
Results:
Line 1: ‘<display line here>’ Balanced
Line 2: ‘<display line here>’ Unbalanced
.
.
.
Line n: ‘<display line here>’ Balanced
Heres the text file: https://drive.google.com/open?id=1aXHvnP9hQduJA-yCDApbICNdVoVHcFwwJFVG-TdJ4xA
Solution
Hi buddy, please find the below java program. I\'ve added comments for your better understanding
import java.util.*;
import java.lang.*;
import java.io.*;
class Balancer{
//This method checks whether a string is balanced or not
public boolean isBalanced(String str){
Stack<Character> stk = new Stack();
//Pushing the first character on the stack
stk.push(str.charAt(0));
Character top =\',\';
for(int i=1;i<str.length();i++){
//Collect the top most character on the stack (Only if stack is not empty)
if(stk.size()>0)
top = stk.peek();
//If the element is the left brace, push on to the stack
if(str.charAt(i)==\'(\'||str.charAt(i)==\'[\'){
stk.push(str.charAt(i));
}
//If the element is the right bracket, top must be the left bracket.
else if(str.charAt(i)==\')\'){
if(top!=\'(\'){
return false;
}
//If the current element is the left bracket, pop out the top most element on the stack
stk.pop();
}
else if(str.charAt(i)==\']\'){
if(top!=\'[\'){
return false;
}
stk.pop();
}
else{
//Ignore any other characters
}
}
//Stack must be empty in the end
return stk.size()==0;
}
}
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
//Initializing buffered reader to read the lines in the file
BufferedReader br = new BufferedReader(new FileReader(new File(\"D:/assn3data.txt\")));
//Queue to store the answer
Queue<String> answer = new LinkedList<String>();
//Variable to store line number
int no = 1;
String str;
//Instantiating Balancer class
Balancer b = new Balancer();
//Iteratign through all the lines in the file
while((str=br.readLine())!=null){
boolean balanced = b.isBalanced(str);
String t = \"Line \"+no+\": \"+str+\" : \"+(balanced?\"Balanced\":\"Unbalanced\");
//Adding answer to the queue
answer.add(t);
no++;
}
//Iterating through all the elements in the queue and printing the answers
for(String x : answer){
System.out.println(x);
}
}
}
INPUT :assn3data.txt
()()((()))[][][][[]]
(a+b)*(c+d)
{{}}}
(This is example of unbalanced[] paranthesis
OUTPUT:
D:\\>java Main
Line 1: ()()((()))[][][][[]] : Balanced
Line 2: (a+b)*(c+d) : Balanced
Line 3: {{}}} : Unbalanced
Line 4: (This is example of unbalanced[] paranthesis : Unbalanced


