CSCI 241 Labs: Lab 2
Write, Fix, Fix, Fix ...


There are 5 checkpoints () in this lab, including the cleanup checkpoint. You and your partner should work together using just one of your accounts. So that one partner doesn't dominate the computer, CHANGE WHO IS IN CHARGE OF TYPING AT THE COMPUTER AFTER EACH CHECKPOINT! If you need help with any exercise, raise your hand.

Getting Started

Copy the directory Lab02 from /home/student/Classes/Cs241/Labs to your account. If you have forgotten how to do this, refer to the steps in Lab01.

Change directories into Lab02 and start BlueJ by entering bluej&.

Breaking Code on Purpose

Open the project FixCode and look at the implementation of the class ComputeArea. This is the same program that is Listing 2.1 in your textbook. On the screen, however, BlueJ color-codes the program in its own way. Discuss with your partner what each color selection means. Even this early in the semester, you see code that contains many parts: reserved words, variables, operators, assignment statements and comments.

It will help us reference parts of the program if we make line numbers visible. Follow these steps to do so:

When you return to the editor window for the program, line numbers should appear on the left of each line of code, including lines that are empty. This setting will now be in place every time you open BlueJ on your lab account.

Compile and run this program to make sure it runs correctly. What does it print if the circle has radius 20?

Take out a sheet of paper and write Forced Errors at the top. Make 3 columns, labelled:

Line #, Change Made, Error Message

Now's the time to have some fun! Good programmers are not only good detectives, but they are also very creative. For this checkpoint, you and your partner should brainstorm on the many ways that one could make a mistake when writing this program. We want you to make at least 10 different kinds of changes to the existing code just to see what happens. Make only one change at a time. For each error you create, record the related information in your table. Some changes that you try may not result in error messages, so you can write "none" in that column for that change. See if your change made a difference to the running of the program (if it compiled successfully, of course). If BlueJ detects more than one error from a single change, please record the first error message in the list.

Some ideas for changes include:

revising spacing mispelling reserved words
mispelling a variable name removing a word, operator or other character
adding a word, operator, comma or other character splitting a statement between lines of code
changing capitalization changing bracket types

Try to make as many different types of changes to the code as you can. (The more you learn from this checkpoint, the easier the following ones will be.)

1 Show us your written list of changes and errors.

  1. Which of your changes caused syntax errors?
  2. Which of your changes caused logic errors?
  3. Did any of your changes make no difference to the running program?
  4. What were the most unhelpful error messages?
Close the ComputeArea class.

Correcting Syntax Errors in Existing Code

Before continuing, switch which partner controls the keyboard.

Still within project FixCode, examine the implementation of the RightTriangle class. This program is supposed to prompt the user for the lengths of two sides of a right triangle, then it calculates the length of the hypotenuse and reports it. First, let's review the formula to calculate the length of a right triangle's hypotenuse:

If a and b are the lengths of the two sides next to the right angle, we typically see the formula expressed this way:

a2 + b2 = c2
So, the length of c is the square root of a2 + b2.

In Java, we use a special built-in method to calculate square roots named Math.sqrt(double). For example, we can calculate the square root of 18.2 and save it in a variable named root with this line of code:
double root = Math.sqrt(18.2);

Unfortunately, your instructors were having brain cramps when they wrote this program. It is full of syntax errors. Your goal is to (1) find, (2) record and (3) remove all the errors.

You may be able to identify some of the syntax errors without compiling the program. For today's lab, however, we want you record the error messages that Java's compiler produces for common syntax errors. You'll also record how you figured out how to fix them.

Take out a sheet of paper and write Syntax Errors at the top. Make 3 columns, labeled:

Line #, Error Message, Debugging Technique/Solution
Compile the program as many times as necessary, recording the information in each column as you fix the errors that the compiler program detects. Recall that PreLab2 contains a list of some common debugging techniques.

Think about what kind of data you can try to test if the program is working correctly. Is there an easy set of numbers you remember that make up the sides of a right triangle? (Hint: 3-4-5 is one example.) You can search the internet and find instant calculators for this, and try some of those numbers in your program. Your instructor found a calculator at this location:

Hypotenuse Calculator

Choose another set of numbers that you can use to check that your program is working correctly.

2 Show us your written list of syntax errors. Also, show your instructor or lab assistant that the RightTriangle program works and how you chose your test data.
  1. How many syntax errors did you find?
  2. Which were the trickiest to solve?
  3. Did any of these syntax errors match what you discovered in checkpoint #1?
Close the RightTriangle class.

Correcting Logic Errors in Existing Code

Open the Statistics class. Compile it - no errors! But ... it is supposed to ask the user for 5 decimal numbers and calculate their sum and average. That's obviously not happening.

Find room on paper for another list, this time keeping track of the logic (run-time) errors. Make 3 columns, this time labeled:
Line #, Logic Error, Debugging Technique/Solution
As you fix the errors in the program, try to follow the original style of coding. Record each error as you discover and fix it.

Run the program and enter these numbers: 1, 2, 3, 4, 5

Their sum should be 15 and their average should be 3.0 - did it work?

Run the program and enter these numbers: 2.3, 2.5, 1.4, 3.6, 4.4

Their sum should be 14.2, and their average should be 2.84. What do you see instead?

3 Show us your written list of logic errors. Also, show your instructor or lab assistant that the Statistics program works and how you chose your test data.

  1. Explain to us what was logically wrong with Statistics and how you corrected it.
  2. What kind of debugging technique(s) did you use to solve the logic problems?
  3. Do you think the above numbers tested this program completely? Are there types of numbers that this program couldn't handle?
Close the Statistics class.

Fix and Add More

Before continuing, switch which partner is at the keyboard.

Open the editor for the NumSquareCube class. This program also contains errors, but different ones that you had in the previous program (although not so many). Begin by correcting the errors in the existing code, recording the error messages and what they mean, just as you did previously.

Once you have corrected the syntax and logic errors, you will see this output when you enter the number 2 at the keyboard:

Number Square Cube
------------------------------
2 4
This is fine so far, but you can see that the last column has no value. Not only that, but we want the finished program to print 3 rows of numbers instead of 1. The second and third rows will have the same types of calculations for (first number + 1) and (first number + 2). For example, when complete (again, assuming that you enter a 2 at the keyboard), the program should print:
Number Square Cube
-------------------------------
2 4 8
3 9 27
4 16 64
Your job is to finish the program so that it will print 3 complete rows of numbers (only one number will be entered at the keyboard). Don't worry about lining up the 1's columns; we learn how to do that soon. Keep track of any syntax and logic errors you see as you complete the program.

4 Show us your written lists of errors. Demonstrate the program to your instructor or lab assistant with different input values.

Close the NumSquareCube class. Close project FixCode.

5 Show us that you have logged out, cleaned up, pushed in your chairs and turned off your monitor for this last checkpoint.

End of Lab