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&.
It will help us reference parts of the program if we make line numbers visible. Follow these steps to do so:
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 MessageNow'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 |
1 Show us your written list of changes and errors.
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 = c2So, 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/SolutionCompile 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: 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.
Line #, Logic Error, Debugging Technique/SolutionAs 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.
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 CubeThis 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:
------------------------------
2 4
Number Square CubeYour 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.
-------------------------------
2 4 8
3 9 27
4 16 64
4 Show us your written lists of errors. Demonstrate the program to your instructor or lab assistant with different input values.