Lab: Experimenting with the Debugging of Conditional Algorithms
Instructions:Omitted
Getting Ready:Omitted
1. Syntax Errors:
This part of the lab will help you understand and fix simple
syntax errors.
-
Compile
PostfixCalculator.java
.
-
What error message was generated by the compiler?
PostfixCalculator.java:42: not a statement
ok == true;
^
1 error
-
What line in
Postfix.java
contains the error?
42
-
Fix the error.
-
What change did you make?
I changed the ==
operator to the =
operator
(i.e., the boolean equals operator to the assignment operator).
-
Compile
PostfixCalculator.java
.
-
How many errors were generated by the compiler?
10
-
Why did the number of errors increase?
The "original" error made it impossible for the compiler to detect
any of the others.
-
Scroll up to the first error. It should be:
PostfixCalculator.java:29: cannot find symbol
symbol : class booleaN
location: class PostfixCalculator
booleaN ok;
^
-
Why doesn't the compiler recognize the symbol
booleaN
?
Java is case sensitive.
-
Change
booleaN
to boolean
in line 29.
-
Compile
PostfixCalculator.java
.
-
How many errors were generated by the compiler?
9
-
Scroll up to the first error. It should be:
PostfixCalculator.java:38: cannot find symbol
symbol : variable operator
location: class PostfixCalculator
operator = Text.charAt(args[2], 0);
^
-
What line does this error message say is causing the problem?
38
-
In fact, there is nothing wrong with this line. Instead,
look at line 30 in
PostfixCalculator.java
where the variable operator
is supposed to
be declared.
-
How is
operator
actually spelled?
-
Fix this spelling error.
-
What did you learn about error messages and the cause of the
error from this example?
Sometimes the error is not always what it seems to be. In this case,
the program didn't use a variable name consistently. However, the mistake
was in the declaration of the variable not the use of the variable.
-
Compile
PostfixCalculator.java
.
-
The first (and hopefully only) error should be:
PostfixCalculator.java:52: incompatible types
found : char
required: boolean
else if (operator = '-')
^
1 error
-
Why did the compiler generate an "incompatible type" error?
In othe words, why didn't it generate an error telling you
that the
=
operator should really be the
==
operator?
The first thing the compiler does is looks at the =
operator inside of the parentheses. It knows that the =
operator is the binary assignment operator, so it checks to see if the
two operands are of the same type. In this case, the right-side operand
is a char
and the left-side operator is a boolean
.
-
Change the
=
operator in line 52 to the
==
operator (and make sure you understand why).
-
Compile
PostfixCalculator.java
.
-
Hopefully, the compiler didn't generate any errors.
Does this mean that the application will now work correctly?
No, it only means that the application contains no syntax errors. It could
still contain logic errors.
2. Logic Errors:
This part of the lab will help you learn how to find and fix logic
errors in conditional algorithms.
-
Run the application with the command-line parameters
8 3 -
.
-
What was output?
Infix Expression: 8 - 3
Result: 11
-
Obviously, this output is not correct. To help find out why,
after the statement:
result = leftOperand - rightOperand;
add the following "debugging output" statement:
System.out.println("Subtraction: " + leftOperand + " " + rightOperand);
-
Run the application with the command-line parameters
8 3 -
.
-
Notice that this statement did not generate any output. That can
only mean that it wasn't executed. To see why, add the
following "debugging output" statement above the first
if
.
System.out.println("Operator: " + operator);
-
Run the application with the command-line parameters
8 3 -
.
-
What "debugging output" was generated?
-
This means that the operator is the '-' character. So, there must
be another reason that the subtraction block is not being executed.
One way to find the logic error is to add more "debugging output"
statements. Specifically, replace the big
if
-else
block with the following:
DebugOutput.java
(Fragment: calculations)
-
Run the application with the command-line parameters
8 3 -
.
-
What "debugging output" was generated?
Operator: -
Addition: 8 3
-
The "debugging output" indicates that the code
in the addition block was executed, rather than the
code in the subtraction block. This is because the
if
statement is if (operator != '+')
when it should
be if (operator == '+')
. Fix this mistake (and make
sure you understand why).
-
Run the application with the command-line parameters
8 3 -
.
-
Is the output correct?
Yes.
-
You might now be tempted to delete all of the "debugging output"
statements to make the "normal output" easier to read. That would
be a mistake since you may need it again later.
Instead, "comment out" the debugging output by putting a
//
in front of each.
-
Run the application with the command-line parameters
5 7 +
.
-
Is the output correct?
Yes.
-
Run the application with the command-line parameters
4 9 *
.
-
What is wrong with the output?
The infix expression is correct but the result is 0 when it should be 36.
-
Remove the comment symbols from all of the "debugging output" statements
so that they will be executed again.
-
Run the application with the command-line parameters
4 9 *
.
-
Is the operator correct?
Yes.
-
Why isn't the multiplication block executed? (If you can't figure
it out, ask for help before proceeding.)
The &&
operator should be the ||
operator.
-
Fix this mistake.
-
Run the application with the command-line parameters
4 9 *
and
4 9 x
to make sure it works for both.
-
Run the application with the command-line parameters
12 5 /
.
-
Is the output correct?
Yes.
-
Run the application with the command-line parameters
12 5 %
.
-
Is the "normal output" correct?
No. The operator is correct but the expression isn't. In addition,
it says the operator is invalid.
-
The "debugging output" indicates that the remainder block is
being executed.
-
What is wrong in the remainder block? Hints: (1) Carefully
compare this block to the other blocks. (2) There are two
mistakes. (If you can't figure it out, ask for help before
proceeding.)
It should not include the statement ok = false;
and the *
operator should be the %
operator.
-
Fix these mistakes.
-
Run the application with the command-line parameters
6 3 $
.
-
What is wrong with the output?
It should contain the message "Invalid Operator!" since
the $
is not an operator that the program understands.
-
Fix this mistake by adding an
else
block.
Do not add any "output" statements.
(If you can't figure it out, ask for help before
proceeding.)
-
What code did you add?
-
"Comment out" the debugging output by putting a
//
in front of each.
3. Duplicative Code:
This part of the lab will help you understand one of the problems that
can be caused by duplicative code.
-
Run the application with the command-line parameters
6 3 +
.
-
What was output?
Infix Expression: 6 + 3
Result: 9
-
Run the application with the command-line parameters
6 3 $
.
-
What was output?
Infix Expression: 3 $ 3
Invalid Operator!
-
This error message from the program is very confusing. What is the problem?
It says the operands are both 3 but the first one should be 6.
-
Fix the error. (If you can't figure it out, ask for help before
proceeding.)
-
What change(s) did you make?
I switched the first rightOperand
to leftOperand
in the call to println()
.
-
The
if
block and the else
block now
contain duplicate code. Fix this problem. (If you can't figure
it out, ask for help before proceeding.)
-
What change did you make?
I deleted the statement containing
"Infix Expression: "
from the
else
block and moved the statement containing the same line from
the if
block to above the if
statement.
-
Why is this a better implementation?
Because now that the code duplication has been eliminated, I don't have to
worry about fixing the same mistake in more than one place.