CS 101 Laboratory #5
Debugging

Objective: To gain experience using a built-in debugger; to continue working with conditionals and loops.
Due date is the beginning of your lab section next week:

Lab prep: Read this lab assignment.

Reading: None

Written Exercises: There are no written exercises this week.

In this lab, you will use the DrJava IDE and built-in debugger to develop and debug Java programs. In particular, you will "step through" some Java code and observe how variable values change, and then develop your own programs that use Java looping constructs to solve programming challenges.

For each program that you write, be sure to include comments at the beginning indicating your name, your lab section, the exercise number, and what the program does. Please turn in a printout of your Java source file for each program that you write and please make sure all pages are stapled together.



  1. This problem requires use of the DrJava debugger as demonstrated in class on Monday. Save the file Debug.java to your cs101 directory in a new subdirectory called hw05. Open the file from DrJava, compile it, and enter debug mode by typing Cmd-D.

    1. Put a breakpoint on the line after the statement d++. (Put your cursor on that line and type Cmd-B.) In the "Watches" pane, enter the variables a, b, c, and d on separate lines. Using the arguments below, run your program. When you do so, DrJava will execute the statement until the breakpoint and then stop (before executing that statement). To continue, click on the "Step Over" button to the right of the "Watches" pane. Report the values of variables a, b, c, and d at each iteration for the following three cases. Also report what gets printed.
         java Debug 2 10
         java Debug 3 5
         java Debug 4 4
      
    2. What does the program compute?
    3. Extra credit: How can the number of steps taken be computed from inputs a and b?

  2. The "Hailstone" sequence starts with any positive integer n, and defines the next value in the sequence as follows: if n is even, the next value is n/2; if n is odd, the next value is 3n+1. (The Collatz conjecture states that for any starting value of n, the sequence will always converge to 1. Although no counterexample is known, the conjecture has not been proven.) For example, for a starting value of n=10, the sequence is 10, 5, 16, 8, 4, 2, 1.

    Write a program HailStone1.java that implements the Hailstone function and prints the sequence of values for an input n. It should take an integer n given as a command-line argument, and print the sequence of values until the sequence reaches 1.

    Run your program in DrJava in the Interactions tab of the Interactions pane (rather than using the Run button). Give a "usage" message that shows how to use the program if no command-line argument is given. Here are some examples of how your program should work:

    > java Hailstone1 7
    7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 
    
    > java Hailstone1 17
    17 52 26 13 40 20 10 5 16 8 4 2 1 
    
    > java Hailstone1
    Usage: java Hailstone1 <n>
    
    A copy of our sample solution class file (without source code :-) is available at Hailstone1sample.class. Save this file to your cs101/hw05 subdirectory (right-click and choose "Save Link As"), and run it from the Interactions Pane to see how the Hailstone1 program should behave.
  3. Copy your code from the above program into a new file called Hailstone2.java. Now, in this version, instead of printing the sequence, develop a function that returns the number of values in the sequence (starting with n given as a command-line argument). Write a main method that calls this function and prints the result. For example:

    > java Hailstone2 17
    Length of Hailstone sequence: 13
    
    > java Hailstone2 21
    Length of Hailstone sequence: 8
    

    As usual, include a usage message if the user does not supply command-line input. Note that all printing happens in the main method, not in the function.

    Make sure your program works for several values of n. Turn in a printout of your Java source code.

    A copy of our sample solution class file is at Hailstone2sample.class.
  4. Copy your code from the above program into a new file called Hailstone3.java. Using a for loop in your main program, now produce a table of the length of the Hailstone sequences. Given command-line input n, print a table with a row for each i, 0 < i <= n, and the corresponding length of the Hailstone sequence.

    Here is an example of how your program should work:

    > java Hailstone3 5
    i:   Length of Hailstone sequence: 
    1    1
    2    2
    3    8
    4    3
    5    6
    

    Turn in a printout of your Java source code.

    A copy of our sample solution class file is at Hailstone3sample.class.
  5. HiLo is a guessing games where one person chooses a random number between 1 and 100 (inclusive), and the other person tries to guess it in the fewest tries. For each guess, the chooser indicates if it is correct, too low, or too high. Suppose that your friend, Sloppy Joe, tries to write a version that lets the user choose the random number to be guessed by the computer program. Unfortunately Joe's program has some errors. His source code is in the file HiLo.java. Save this file to your cs101/hw05 subdirectory. Use the DrJava compiler and built-in debugger to fix the syntax and logic errors so that the program works properly. Add a comment to each line where you fixed an error, and submit a printout of your corrected program.

    A working version is available at HiLoSample.class. Save this file to your cs101/hw05 subdirectory as well, and run it to see how the HiLo program should behave.

  6. About how long did it take you to complete this assignment?

Back to Computer Science 101 Home
Department of Computer Science