Graphic by Keith Ohlfs |
|
When programming in Java, you will often make mistakes typing or designing your program code. These mistakes will lead to two types of errors: Compiler errors, which cause error messages during the compilation of your program, and run-time errors, which allow the program to compile, but cause it to do the wrong thing. These errors are often called "bugs". Knowing how to find and correct bugs in your code (known as debugging) is an extremely important skill for a programmer. In lab we examined some common types of errors. Some common errors are as follows:
However, your lab instructor was up very late at night preparing for the lab and being rather sleepy, she made some errors (less than 10) when writing the code. Your mission is to find the errors and correct the code for her. Here's how to get started:
1. In the download subdirectory of the cs111 folder on Nike, there is a folder called buggyCode. Use Fetch to download this folder to your Macintosh. This folder contains 3 items.
3. Open the java source file by double clicking on the name CS111stick.java in the project window.
4. Try to build the applet code by choosing "Bring up to date..." from the Build menu.
5. A window will appear titled "Build Errors for CS111stick.proj". Several errors will be listed in this window. Note that the compiler will not necessarily report all the errors in the code. Also note that it may not correctly determine what the error is.
6. Double click on one of the error messages. The project manager will highlight the line in the source code in which it perceived an error. This is a good place to start looking for the error, but keep in mind that the error might occur in one of the lines above the line the compiler highlights. If you do not understand the error message, try looking for typographical errors of the sort mentioned above.
7. When you discover an error, correct the code in the .java file and try "Bring up to date..." again. (Make sure you save your corrected file first). You will find that sometimes fixing one typographical error will eliminate 2 or more error messages. However, some new error messages may appear after you fix an error.
8. Create a separate textfile, called Errors.txt (Choose "New" from the File menu) and in it write down what the error was and which line it occured on. E.g. you might say "Line 28 was missing a semicolon" if that was the case. Save this textfile.
9. Repeat steps 6 through 8 until the program compiles.
10. Look at the applet by running the AppletViewer and choosing "Open Local" to open the CS111stick.html file. You will find that there are some run-time errors.
11. Correct the run-time errors in the source code, and write down what errors you found and what line they occured on in Errors.txt.
12. When you are finished, make sure the buggyCode folder contains the following items: