Compiling, running, and debugging tutorial
In Step 5, you fixed an error in the divideValues()
method. Now, when you run the program again, you may notice the statement saying whether the first value is odd or even is incorrect.
For example, if you enter 4
into the Value 1 field, the program reports it is an odd number. However, if you enter 3
, the program says that the value is even. In this last step, you will find and fix this error.
To find this error, we'll use the Evaluate/Modify dialog box to evaluate the method that determines if the number is odd or even. Then we'll set a watch on the result returned from the method to see if it's printing to the screen correctly.
oddEven()
method in Frame1.java.
Notice that a variable name also includes the text OddEven.
To find the method, you can turn the Case Sensitive option on in the dialog box or search for: oddEven(
oddEven(valueOneDouble);
3
in the Value 1 input box and 4
in the Value 2 input box when the program's UI is displayed. Click the Compute Values button. The focus returns to the debugger.
Choose Run|Evaluate/Modify to open the Evaluate/Modify dialog box.
Enter oddEven(valueOneDouble)
in the Expression input box. Click Evaluate. You'll see that the method returns true.
Close the Evaluate/Modify dialog box.
Now, we'll step into the method in order to evaluate what the true
value means.
valueOneOddEven
.
oddEven()
method, the value of valueOneOddEven
is true,
because the value was initialized to true
. (To see the initialization, use the Search|Go To Line command to go to line 62 in Frame1.java.
Then, use Run|Show Execution Point to return to the cursor location.)
valueOneOddEven
remains true.
Is this correct? Does the result of (3 modulus 2)
equal zero? It actually does not equal zero, and the value of valueOneOddEven
should be set to false.
Right-click valueOneOddEven
in the Data watches view and choose Change Value to test this theory. The Change Value dialog box is displayed.
Enter false
and click OK. The value of valueOneOddEven
is set to false
. You just changed the method's returned value from true
to false
.
Click OK to close the dialog box.
if
statement in the next line of code.
if
statement. It is actually quite simple:
If valueOneOddEven is true, print the message stating that
the number is even. However, if the value is false, print
the message stating that the number is odd.
else
statement, the line that states: "If the value of valueOneOddEven
is false, print the message stating the number is odd."
oddEven()
method in the structure pane to go to the location of the method in the editor. (You may have to scroll the structure pane to see the method.)
true/false
results assigned correctly? If you look closely, you'll notice that the true
and false
assignments are actually mixed up. The code is stating that if the modulus equals zero, the return value is false
and the number is odd. If the modulus does not equal zero, the return value is true
and the number is even. These statements should actually be reversed, so that the code will read:
if (valueOneDoubleResult % 2 == 0.0) { valueOneOddEven = true; } else valueOneOddEven = false;
true
and false
values on lines 277 and 279.
To save your changes and run the program,
3
in the Value 1 input box and 4
in the Value 2 input box. Click the Compute Values button. The result is correct! The program now correctly informs you that Value 1 is an odd number.
In the next step, you will see what happens when a runtime exception is generated.