CS210: Intermediate Computing/Data Structures (Java)

Lab #4


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 Lab4.zip on your removable media (floppy disk, Zip disk, or memory stick) creating a directory named Lab4.

Open the Dr Java Application on your PC. 

Create a new Dr Java project in the Lab4 directory by selecting the menu Project -> New… naming the project file Lab4 and saving it in your new Lab4 directory.  (Dr Java automatically opens an Untitled file in the editor window which you will ignore.)

Using the menu File-> Open, open the file BalanceCheck.java.  Dr Java will automatically include this source file in the Project.  Use the menu Project -> Save to save the updated project file.

Study the java.util.Stack class in the Java 5.0 API Documentation on the Sun website so that you know how to use it.

Lab 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.

Using the menu Project -> Compile Project, compile the program.  Run your program on the three test files provided: TestClassBad1.txt, TestClassBad2.txt, and TestClassGood.

Show your code to the TA before leaving the lab.  When you are through with the lab, close the Project using the menu Project -> Close.  Be sure to remove your media and take it with you.  If you have not finished the code, finish it by the next lab period. 

Before you leave, have your TA check your code in progress.  If you have not completed it, finish it before the next lab session to include a hard copy with your lab report.

Lab Report

Write a document that answers the following questions.   Your lab must be printed (not handwritten). 

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.

Note:  The assignment is due at the BEGINNING of your next lab.  No late assignments will be accepted.  Emailed assignments will not be accepted.  If you are not going to be in lab on the due date, you can turn the assignment ahead of time to the CS210 TA box in the CS department office.