Assignment 7 - Debugging and Testing
In this homework, you'll work on a simple calculator applet. The Calculator program that comes with Windows is handy, but it seems to me that it doesn't work quite right. What I want is a calculator that works like my old adding machine: Type a number, press +, and the value is added to a running total;Type another number, press -, and the new value is subtracted from the running total.
The Calc7 applet, shown here, works as I've described. All you have to do, is download the source code, compile and test it, and you're done. Of course, since this week's lesson is on "Bugs and Exceptions", you know there has to be more to it than that. Guess what? You're right.
This homework covers the material from Lesson 7 where you learned about several kinds of errors, and how to fix them, and a little from Lesson 8 [exception handling]. You should have read through the material before getting started, otherwise, you'll find yourself wasting a lot of effort.
To complete the homework, you'll have to understand how to locate and fix:
- Structural, compile-time errors.
- Typographical errors.
- Syntax errors.
- Type errors.
You'll also have to understand how to locate and fix runtime errors such as null-pointer assignments, and how to use Java's try-catch exception handling to process:
- ArithmeticException
- NumberFormatException
Rather than submitting the finished source code for your program, I want to you to keep notes as you debug and test your program. At each step, I'll ask you some questions. Write down and submit your answers to those questions.
Step-By-Step Instructions
Step 1
Fix the structural errors. When you first compile, you will see several error messages. Not all of these error messages are valid. There are two structural errors in the program. When you fix those structural errors, some of the previous messages will go away.
To fix structural errors, recall, you begin by commenting out the entire body of your class, and then, step-by-step, reveal more and more, until the errors are obvious.
What are the two structural errors, and what did you do to fix them? [Don't fix any other errors until you take care of these two errors.]
Step 2
Fix the typos and syntax errors. After fixing the structural errors, your program should have between ten and fifteen new compile-time errors. [Some compilers report more errors than others because of the cascading error problem.] These errors fall into three categories. For each of the categories, tell me what each of the error messages said and what you did to fix the problem.
Here are the three categories:
- Typographical errors, such as spelling the name of a variable, type, or class incorrectly.
- Syntax errors, such as using a type name where an identifier is expected, or using a variable that hasn't been defined or initialized.
- Type errors where you attempt to store a value of one type in a variable of another type, or when you attempt to pass a value of one type to a method which is expecting a value of another type.
Step 3
Fix the run-time error. After you fix all of the compile-time errors, create an HTML page and take a look at the applet in Appletviewer. Rather than the calculator you expect, you'll get a runtime error.
Locate and fix the runtime error, and then tell me what the error was, and tell me how you fixed it.
Step 4
Add some error checking. Once the program works, test out all of the buttons. Do they work as you'd expect? Most of them do, but not all.
I'd like you to fix two problems:
- The Backspace key throws an exception when you backspace and there are no characters displayed. Use a simple if() statement to fix that.
- When you enter certain combinations of numbers [such as a number beginning with a decimal point on IE], you get a NumberFormatException. Use try-catch to intercept and fix this problem.
In each case, show me where the problem occurred, describe the circumstances where it occurred, and tell me what you did to fix it.
Finishing Up
Post your applet. Once you've tested your applet locally, add it to the Assignment 7 HTML file on your Web site. Remember that you must send the .class file as well as an updated HTML file. When you use FTP to send up your .class file, remember that it must be transferred using binary mode, not ASCII.
|