CS 161 Lab 4


This lab will explore program debugging with assertions. Specifically: The assert keyword is used to implement assertions and validate the input or assumption in Java. We can use the assert keyword in two formats:
assert booleanExpression;

assert booleanExpression : errorMessage;

The first form "assert booleanExpression" is used to test the boolean expression. If the boolean expression is false then Java throws AssertionError and your program terminates. The second form of assert keyword "assert booleanExpression : errorMessage" is more useful and provides a mechanism to pass an appropriate message when the assertion fails and Java throws AssertionError.

To enable the Java keyword assert in Eclipse you need to do the following steps:

  1. Form the menu bar, select Run -> Run Configurations....
  2. Select Arguments tab.
  3. Add -ea to VM arguments.
  4. Click Apply.
  5. Click Run.

Follow along with the demonstration

Lab Assignment

You will need the file called Recit4.java. It is a buggy implementation of the first assignment P1. You will also need tweets.txt and stopWords.txt as input files.

Your lab assignment is to fix bugs in the four methods inside Recit4. Your TA will show you how to fix some of them, and you will fix the rest. Please complete this portion of the lab individually. You are always welcome to bring any lab material and questions to office hours.


This lab is worth a total of 2 points broken down as:
Demo/Attendance: 1 point
Effort: 1 point