Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help wit this assignemnt. Write a program to read in a text file (of your

ID: 3796142 • Letter: I

Question

I need help wit this assignemnt.

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.

YOU MUST INSTANTIATE AN OBJECT OF YOUR OWN CLASS. NO MORE USING "public static" methods from this assignment on.

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

Explanation / Answer

CODE:

/* Bunch of import statements */
package filetest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Stack;
import java.util.Queue;
import java.util.Scanner;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
*
* @author Balli Gujral
*/
public class FileTest {

String filename;
Stack s = new Stack();
/* Needed definations of Queue because of abstract methods INGORE THIS */
static Queue q = new Queue() {
@Override
public int size() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean isEmpty() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Iterator iterator() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object[] toArray() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object[] toArray(Object[] a) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean addAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean add(Object e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean offer(Object e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object remove() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object poll() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object element() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object peek() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean removeIf(Predicate filter) {
return Queue.super.removeIf(filter); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Spliterator spliterator() {
return Queue.super.spliterator(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Stream stream() {
return Queue.super.stream(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Stream parallelStream() {
return Queue.super.parallelStream(); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void forEach(Consumer action) {
Queue.super.forEach(action); //To change body of generated methods, choose Tools | Templates.
}
};
/*Constructor*/
private FileTest(String f)
{
filename=f;
}
/*File Read Method*/
public void fileread() throws FileNotFoundException
{
  
FileReader fin = new FileReader(filename);
BufferedReader bin = new BufferedReader(fin);
try {
while(bin.readLine() != null)
{
q.add(bin.readLine());

}
  
} catch (IOException ex) {
Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
}
  
}
/*File Test Method*/
public int test(String ch)
{
int i;
for(i =0;i<=ch.length();i++)
{
char check = ch.charAt(i);
if (check == '[' || check == '(')
{
s.push(check);
}
if (check == ']' || check == ')')
{
s.pop();
}
}
if(s.isEmpty())
{
return 1;
}
else{
return 0;
}
  
}

public static void main(String[] args) throws FileNotFoundException {
  
Scanner reader = new Scanner(System.in);
System.out.println("Enter filename : ");
String f;
f = reader.nextLine();
System.out.println(f);
FileTest fin;
fin = new FileTest(f);
System.out.println("File read in: "+f);
fin.fileread();
for(int i=0;i<=q.size();i++)
{
String c = (String) q.remove();
System.out.println("Line "+i+": "+c);
int ch = fin.test(c);
if (ch == 1)
{
System.out.print("Balanced ");
}
else
{
System.out.print("Unbalanced ");
}
}
}

  
}


DROP DOWN A COMMENT IF ANY DOUBTS..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote