Tutorial Step 05 Step 07

Compiling, running, and debugging tutorial

Step 6: Fixing the oddEven() method

In this step of the tutorial, you will find the last of the three runtime errors. You will use the Evaluate/Modify dialog box to evaluate a method call, step into and over a method, set a watch, and change a boolean value on-the-fly to test a theory.

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.

  1. Use the Find/Replace Text dialog box to locate the call to the 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(

  2. Set a breakpoint on this line:

    oddEven(valueOneDouble);

  3. Click the Debug button.

  4. Enter 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.

  5. This step is for JBuilder Professional and Enterprise users only.

    Choose Run|Evaluate/Modify to open the Evaluate/Modify dialog box.

    Tip: You can also right-click in the editor and choose Evaluate/Modify.

    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.

  6. Go to the Data watches view. Set a watch on valueOneOddEven.

  7. Click the Step Into button on the debugger toolbar. When you step into the 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.)

  8. Click Step Into three more times to step further into the method. This method determines if the value is odd or even. As you step, the value of 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.

  9. This step is for JBuilder Professional and Enterprise users only.

    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.

  10. Click Step Out     to step out of the method and return to the calling location, then click Step Into to trace into the if statement in the next line of code.

  11. Examine the contents of the 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.

  12. Click the Step Into button again. The execution point goes to the else statement, the line that states: "If the value of valueOneOddEven is false, print the message stating the number is odd."

  13. Click the 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.)

  14. Examine the modulus operation and its results. Are the 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;
    

  15. Switch the true and false values on lines 277 and 279.

To save your changes and run the program,

  1. Save your files.

  2. Click the Reset Program button on the debugger toolbar.

  3. Run the program again.

  4. Enter 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.

  5. Click File|Exit to exit the program. Remove the Application1 tab.

In the next step, you will see what happens when a runtime exception is generated.

Home Step 05 Step 07