Lab: Experimenting with the Debugging of Conditional Algorithms
Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
Getting Ready:
Before going any further, you should:
-
Depending on your development environment, create either a
directory or a project for this lab.
-
Setup your development environment.
-
Download the following files:
to an appropriate directory/folder. (In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above.)
1. Syntax Errors:
This part of the lab will help you understand and fix simple
syntax errors.
-
Read and understand
InfixCalculator.java
, but don't
change any errors you might find.
-
Compile
InfixCalculator.java
.
-
What error message was generated by the compiler?
InfixCalculator.java:42: not a statement
ok == true;
^
1 error
-
What line in
InfixCalculator.java
contains the error?
42
-
Fix the error.
-
What change did you make?
I changed the ==
operator to the =
operator
(i.e., the relational equals operator to the assignment operator).
-
Compile
InfixCalculator.java
.
-
How many errors were generated by the compiler?
8
-
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:
InfixCalculator.java:25: cannot find symbol
symbol : class booleaN
location: class InfixCalculator
booleaN ok;
^
-
Why doesn't the compiler recognize the symbol
booleaN
?
Java is case sensitive.
-
Change
booleaN
to boolean
in line 25.
-
Compile
InfixCalculator.java
.
-
How many errors were generated by the compiler?
7
-
Scroll up to the first error. It should be:
InfixCalculator.java:36: error: cannot find symbol
operator = JMUConsole.readLine();
^
symbol: variable operator
location: class InfixCalculator
-
What line does this error message say is causing the problem?
36
-
In fact, there is nothing wrong with this line. Instead,
look at line 26 in
InfixCalculator.java
where the variable operator
is supposed to
be declared.
-
How is the variable spelled in the declaration?
-
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
InfixCalculator.java
.
-
The first error should be:
InfixCalculator.java:36: error: incompatible types: String cannot be converted t
o char
operator = JMUConsole.readLine();
^
-
What does this error mean?
The readLine()
method returns a String
but
operator
is declared to be a char
.
-
Why is this a problem even if the user only types one character?
char
is an atomic/primitive/fundamental type
and String
is not. Even if the user only enters a single
character, the readLine()
method returns
a String
.
-
How might one fix this problem?
There are two obvious solutions. One is to change the declaration
of
operator
. The other is to read a
char
.
While the first might seem like the easier solution, it isn't, because
everywhere else in the program, operator
is assumed to be
a char
. Fortunately, the JMUConsole
class
has a method that we can use.
-
Change the call to
readLine()
to a call to
readLineAsChar()
.
-
What code did you add?
operator = JMUConsole.readLineAsChar();
-
Compile
InfixCalculator.java
.
-
The first (and hopefully only) error should be:
InfixCalculator.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
InfixCalculator.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.
-
What file was created (now that the
InfixCalculator
class can be compiled)?
InfixCalculator.class
-
What does this file contain?
The byte code (which is almost machine instructions) for the
human-readable source code in InfixCalculator.java
.
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 inputs
8 - 3
. (You
should understand why, given the way the program is written,
you'll have to respond to three distinct prompts to do so.)
-
What was output?
-
Obviously, this output is not correct. To help find out why,
after the statement:
result = leftOperand - rightOperand;
add the following "debugging output" statement:
JMUConsole.printf("Subtraction: %d %d\n", leftOperand, rightOperand);
-
Run the application with the inputs
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
.
JMUConsole.printf("Operator: %c\n", operator);
-
Run the application with the input
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, at the top of each
if
block, add a debug output statement that identifies the block.
Specifically, at the top of the Addition block, add the statement:
JMUConsole.println("Addition");
and do something similar for the other blocks.
-
Run the application with the input
8 - 3
.
-
What "debugging output" was generated?
Operator: -
Addition
Result: 11
-
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 input
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 input
5 + 7
.
-
Is the output correct?
Yes.
-
Run the application with the input
4 * 9
.
-
What is wrong with the output?
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 input
4 * 9
.
-
Is the operator correct?
Yes.
-
Why isn't the multiplication block executed?
The &&
operator should be the ||
operator.
-
Fix this mistake.
-
Run the application with the inputs
4 * 9
and
4 x 9
to make sure it works for both.
-
Run the application with the inputs
12 / 5
.
-
Is the output correct?
Yes.
-
Run the application with the input
12 % 5
.
-
Is the output correct?
No. The operator is correct but there is no result.
-
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.
It should not include the statement ok = false;
and the *
operator should be the %
operator.
-
Fix these mistakes.
-
Run the application with the input
12 % 5
.
-
Is the output correct?
Yes.
-
Run the application with the input
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.
-
What code did you add?
-
Run the application with the input
6 $ 3
.
-
Is the output correct?
Yes.
-
"Comment out" the debugging output by putting a
//
in front of each.