Notes from CS 312 lecture, Friday 10/11/2013

Today's lecture covered assertions, and verification, testing and debugging. We used the Hangman assignment as an example to illustrate different aspects of testing and debug.   Here are five things you can do for debug and testing.

1. Make a class constant
static final boolean DEBUG = true;

Use this constant to print out data to help you debug, or to make the program behave in different ways. e.g. in the Hangman program, use it to display the name of the movie selected by PhraseBank.  Make sure you set DEBUG to false or delete all the DEBUG stuff before you turn in your work!

String currentPhrase = phrases.getNextPhrase();
if (DEBUG) {
    System.out.println(currentPhrase);
}

2. Instead of allowing the PhraseBank to select a movie for you, hardcode in a movie: So

String currentPhrase;
if (DEBUG) currentPhrase = "The Wild Bunch";
else currentPhrase = phrases.getNextPhrase;

The sample output shows three movies in sequence being selected. For this, you can create a variable that shows which round we are on, say roundNumber. Then ...

String currentPhrase;
if (DEBUG) {
    if (roundNumber ==1) {
        currentPhrase = "The Wild Bunch";
    }
    if (roundNumber ==2) {
        currentPhrase = "Dog Day Afternoon";
    }
}

4. Since you may not want to type in a long sequence of inputs, one convenient thing to do is to create an input file that has all your guesses, one on a line. For example, you input file might be named hangman_input.txt, and contain

T
E
H
R
y
A
B
C
 .
 .
 .
n


where the 'y' and the last 'n' stand for the answer YES and NO respectively, to requests for a new game. This assumes that you know a priori what the phrases will be. To use this input file, on a Linux command line, type

java Hangman < hangman_input.txt

The '<' symbol indicates that the program java should take input from a file rather than from a keyboard (Stdin). You can redirect the output from the console (Stdout) to a file, too, by using '>'.

java Hangman < hangman_input.txt > hangman_output.txt

You can use the Linux command 'diff' (do 'man diff' to know more about it) to compare your output to any golden output file.

5. To make sure that your program is really robust, make sure it works for corner cases, such as phrases containing all 26 letters, only one letter, and with only one letter appearing multiple times (such as MMMM)..