Purpose

This lab gives you experience developing a class with a method that uses a Stack.

Pre-Lab Assignment

Do the following activities before coming to the lab:

Download, save, and unzip the file delimiter_pairs.zip on your removable media (floppy disk, Zip disk, or memory stick), creating a new directory for this homework assignment.

Study the java.util.Stack class in the documentation on the Oracle website so that you know how to use it.

Homework Activities

You will write a method that checks a file to determine that each opening delimiter '{', '[', and '(' is followed later in the file by the correct closing delimiter '}', ']', and ')' respectively following the Java syntax rules for proper nesting. This problem represents one portion of the design of a compiler that needs to check that all such pairs of delimiters are properly balanced and nested.

You need to design and add code to the main method of the BalanceCheck class in three places as indicated by the comments:

  1. Your code must instantiate a Stack to contain Character class objects using the java.util.Stack class.
  2. Your code in the "for loop" must pick each character from the String "line" assigning it to c1. If it is an opening delimiter ('{', '(', or'['), push it on the stack. If it is a closing delimiter ('}', ')', or ']'), pop a value off the stack into c2 and check to see it this pair of delimiters is a valid matching pair. If it is not, set error to true and break out of the "for loop" with c1 and c2 containing the values of the mismatched characters.
  3. Your code after the exit from the "while loop" must check the error flag. If it is true, print a message displaying the mismatched pair c1 and c2. Otherwise, check the stack to see if it is empty. If it is not, print a message that there are missing closing delimiters. Otherwise, print a message that the balance check is OK.

Depending on your choice of IDE, compile the program. Run your program on the three test files provided: TestClassBad1.txt, TestClassBad2.txt, and TestClassGood.

Lab Report (in memo.txt)

Answer the following questions related to what you did in this week's lab.

What results did your program provide for each of the test case files?

Study the nesting of the delimiters in each file. Did your program correctly identify the problem in the two bad files?

Note: You should work alone on the lab report.