CS 134 Test Programming Debugging Hints

How can I tell if my encrypt code (or encode code) is working?

Although the handout discusses encrypt before decrypt and encode before decode, you might actually want to implement decrypt first followed by encrypt and then decode followed by encode. The reason is simple. You can check you decrypt code by trying to decrypt some of the sample images we have provided on the course web site. You won't be able to really check your encrypt code, however, until your decrypt code is working. Encode and decode are similar.

Is that image really black?

Many of you are likely to end up with images that look black displayed in one or more of the ImageViewers in your program's interface. Sometimes these images will actually not quite be black. For example, if you display an image that was produced by taking just the low order bits of every pixel of another image, it will look black even though it contains interesting information. The problem is that there is no way your eyes will be able to tell the difference. Fortunately, there is a fairly easy way to examine such an image more carefully.

We included a "Save" button in the ImageViewer class we provided. If you use this button to save one of these black images (or any other image that you want to know more about), you can then use other programs to examine the image. In particular, you could run the program you completed for Lab 8, open the saved copy of the black image, and then either display the histogram of the image or expand its range.

This technique can be used to verify that many parts of your program are working correctly. For example, if you save a copy of an image produced by your BitClearer and then use your Lab 8 code to display the histogram, the resulting histogram should looked striped since every other brightness value should be unused.

Debugging the StringConverter Class

In one of the debugging tutorials we had you do in lab, we showed you how to use BlueJ to test the methods of a class independently before trying to incorporate that class into a larger program. These techniques can be very handy when working on the StringConverter class, so we thought we should offer the following quick review.

First, to use BlueJ's interface to test a method, the method has to be declared "public". Two of the methods declared in StringConverter, convertFromInt and convertToInt, should actually be declared "private" in your final code. Initially, however, you should declare them as "public" to make debugging possible.

Once you think the code for a method is ready to be tested, compile the class and then select the "new" item from the menu that appears when you press "ctrl" and point the mouse at the icon for the class. For example, the image below shows how to construct a StringConverter so that you can test its methods.

After the StringConverter object is created, you can invoke any of its method by selecting the method name from the menu that appears when you ctrl-click on the red icon that appeared when you constructed an object as shown below:

After you do this, a dialog box will be displayed in which you can type the parameters to the method you want to test. For example, to test the convertFromInt method on the value 85 you would type the value in the dialog box and then press "OK" as shown below:

You can also enter character values when testing this method. For example, if we had typed 'U' instead of 85 in the dialog box, the result would be the same since 85 is the value used to represent a U in ASCII. Note that the single quotes before and after the U must be included in what you type in the dialog box if you do this.

After you enter a parameter value and press OK, BlueJ will display the result of the method's execution in a new dialog box like the one shown below:

This dialog box doesn't actually provide much useful information because the result of the convertFromInt is an array. If, however, you double-click on the box containing the array, then BlueJ will pop up a window like the one shown below displaying the contents of the array.

If the array is too big to fit in the dialog, you can use the scroll bar to see the remaining elements (unless the array is very, very big, in which case BlueJ only includes the first few dozen and last few dozen values).

You can also use this technique to test a method like convertToInt which expects an array rather than a single value as a parameter. To describe the array to be processed you simply type the values separated by commas and surrounded by curly braces as shown below: