CS 2102 - Bterm 17

Lab 4 - Debugging Programs

Thank you to Prof. Kathi Fisler for designing the lab exercises for this course.

Lab Objectives

The context for this lab is a program that models a simple voting system. There are two starter classes: VoteBooth.java gives you a user interface through which you can cast votes and see election results, while VoteData.java stores all the data about an election (the names of candidates and the votes that have been cast).

Preliminaries

What you should do

Task 1: Adding a Main class and a main method

Up until now, nothing "happens" when you run one of your programs. We have relied on test cases to run your code. In a realistic system, testing and running the system are separate activities. For the voting system, we would like the screen to come up automatically when we run the program.

Create a class called Main. In it, put a method with the following header:

  public static void main(String[] args) {

    // what to do on startup goes here

  }
Inside the main method, you can write whatever code you want. For today's exercise, you want the main method to create a new VoteBooth object, and activate the screen() method on it. When you run the program, Java automatically starts execution with the main method. Try it and see.

Task 2: Tracking down bugs

With your screen running, you can cast votes and ask for election results. Try (1) voting for Husky then (2) asking for the winner (by entering *W). Notice that the reported winner is Gompei. Now you have to figure out why.

The goal here is not just to find the error(s) and make the code run properly. The goal is to practice techniques for finding errors. Even if you can visually spot the error(s) quickly, try using the techniques given below to "discover" them afresh. This will teach you which techniques work well in different situations.

In general, there are three strategies to take when you realize your code isn't working:

Write Test Cases

Working through test cases as much as you can is much better professional practice: it leaves you with a set of tests that can continue to check your code as you add features, make modifications, and so on. Working through tests also forces you write small, focused methods, rather than complicated methods with lots of variables. This is also very good professional practice.

Writing tests should be how you identify which specific method in a more complex computation is going wrong. Once you've identified the method and a couple of specific inputs that show the erroneous behavior, you may need to move on to one of the other techniques to locate the mistake within the method (though you can often just spot the error directly at this point).

So you should write test cases first. Since we've been practicing this skill all along, we're going to skip it today in order to give you time during this lab period to practice using the debugger. If you had written a set of good test cases, you would have determined that there are problems with two of the methods in the VoteData class: countVotes() and winner(). Let's use another strategy to try to determine what those problems are.

Trace Through Code by Hand

Once you know which method is failing, try evaluating the code in that method by hand. On a piece of paper, write down the names of all the variables and their current values. As you walk through the code step by step, update the variables' values on your paper, looking to see when one of the values is other than what you expected. This can often help you find the error. Again, in the interest of time we'll skip the "by hand" method of tracing code today, to focus on how to automate the tracing process by using a debugger.

Use the Debugger

A debugger is a tool that helps you with the process of tracing through your code. You can set up a debugger to walk you through your code statement by statement (seeing how the values change). You can tell it to run to a certain point and then start going step-by-step (called setting breakpoints), and other handy options.

Here are links to tutorials on the debuggers provided with Eclipse and DrJava:

Eclipse Debugger Tutorial

DrJava Debugger Tutorial

Grab a tutorial, grab a friend, cluster up and have the staff show you how these tools work. Try to find the voting system bugs using your debugger. (The voting code also contains a broken method called longestStreak which gives you another option to practice debugging.) Consider your completion of this lab successful if you leave knowing how to

If you aren't able to finish tracking down the bugs (with the help of the debugger) by the end of the lab period, try to complete the exercise on your own. Being familiar with the use of a debugger is expected in higher-level CS courses.

What to Turn In

You don't need to turn in any files for lab this week. Make sure you signed the attendance sheet to get credit for this lab.