CSE 490c - Homework 3

Due: Friday, April 30, 2:30 pm, over the web (see turn-in details below).

The goal of this homework is to give you experience writing Makefiles to control recompilation of a project and to debug a program using a command line debugger.

The same collaboration rules from previous assignments apply: You should write your answer to this homework alone, but feel free to consult with your friends about Unix in general.

Tasks

  1. Suppose one of your friends was taking CSE 142 last quarter and wanted to show off his final project, a graphical simulation of Critters living, eating each other, reproducing, and dying. Unfortunately, despite the fact that his code is fairly high quality, it is still a little buggy and doesn't quite work. After hearing about the superior debugging skills you are picking up in 490c this quarter, he has come to you for help!

    As described below, you will make it easier to build and run his project by putting together a Makefile, and then you will use jdb to find and fix the bugs. Start by downloading his project1 from the link below and extracting it into a new hw3 directory.

    http://www.cs.washington.edu/education/courses/cse490c/04sp/homework/hw3/gobble.tar.gz

  2. Within the src directory, create a Makefile with at least the rules listed below. You may wish to use variables like SRCS and OBJS (among others) to make the rules easier to write.

    1. Define a rule to compile any single Java source file in the project (by name) into its corresponding class file(s). For this to work, you will need to include ../lib/uwcse.jar on the classpath, since the project uses the graphics library stored in this jar file. Since we'll be debugging the project, you may also wish to compile with the -g flag. (Test this with 'make Critter.class'.)

    2. Define an 'all' rule to compile all the source files in the project. This should rely on the first rule you wrote, and you should make this the default rule to execute if make is run without any arguments. Running this rule when all class files are up to date should not cause any recompilation to occur.

    3. Define a 'run' rule to run the main method of the Gobble class. Before debugging the code, running this rule should result in an ArrayIndexOutOfBoundsException.

    4. Define a 'clean' rule to remove all the class files created during compilation. Be sure to delete inner classes as well.

    5. Having the Javadoc documentation available as HTML will be useful for the second half of the assignment. Define a 'javadoc' rule to run javadoc over all the source files in the project. You should have it save the output files into a new doc directory adjacent to src and lib, which your Makefile should create if it does not exist.

    6. You may also be interested in the number of lines of code in the project. (Don't worry about whether "lines of code" include blank lines or comments for this rule; just counting all the lines in all the source files is good enough for a rough metric.) Define a 'loc' rule to print this number.

  3. Now that you have a usable build environment, use jdb to find and fix the bugs preventing your friend's project from working. In particular, there are four bugs that will be evident when starting the program and using the 'a' key to start the animation. There may in fact be more than four bugs in this code (hey, no one's perfect), but you are expected to fix these four obvious bugs as part of this assignment. As part of the turn in, you will include a text file with a short English description of how you found each bug (at a conceptual level), as well as how you fixed it.

    Notes: You should not need to learn the entire codebase for this assignment! The bugs in the code are realistic mistakes that might be made, but they are also deliberately shallow. You should be able to discover and resolve them by tracing execution with the debugger and by reading invariants for relevant methods in the Javadoc. You may choose to get an overview of the codebase and how to run the Gobble program by skimming the included project.html file, but this is not a requirement.

    For the purposes of this assignment, do NOT refer to actual project solutions or starter code on old 142 web sites!

    1. Run Gobble within jdb to see the first bug, which is the ArrayIndexOutOfBoundsException mentioned above. You can choose to run jdb from the command line, from a 'debug' rule in your Makefile, or from within Emacs (M-x jdb). (You will again need to specify the correct classpath.) The 'where', 'list', and 'print ...' commands can help you learn about the program context and where the source of the bug is. Determine why the exception is occurring and fix the problem.

    2. Recompile and try to run the program normally. You should see a window appear, but also an exception at the command line. Quit the program using the 'q' key and run it in the debugger again. After learning about the variables in scope related to the exception, you may wish to set a breakpoint early in the method and step through to find the source of the problem.

    3. You're on your own for the next two bugs, which are visible when you try to animate with the 'a' key. A few general purpose strategies:

      • Set breakpoints and step through the last few lines before an exception.
      • Read Javadoc comments of methods being called to find invariants that might not be met, etc.

      Also, I've noticed that the 'catch ...' command doesn't always work reliably when dealing with GUIs; your results may vary. When you finish, you should be able to hit 'a' multiple times to speed up the simulation and watch the ants eat the grass.

What to Turn In

You should turn in a compressed hw3-src.tar.gz file containing the contents of your src folder after running make clean. (That is, I want to see all your source files and your Makefile, but not your class files, Javadoc HTML, or the uwcse.jar file.)

You should also turn in a separate Unix text file containing a short English description of how you found and fixed each bug.

Because there are more things to turn in this time, you should submit these two files over the web via E-submit, not via email. Update, 4/26: I've added everyone to the group for the submission page, so you should be able to turn in now. Let me know if you have any problems.


[Note: Credit is due to Doug Johnson for the original working codebase; bugs were explicitly added (and packages were flattened) by Charlie Reis for this exercise in debugging.]