JMU
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.
  1. Compile PostfixCalculator.java.
  2. What error message was generated by the compiler?


    PostfixCalculator.java:42: not a statement
           ok     == true;
                  ^
    1 error
    
  3. What line in Postfix.java contains the error?


    42
  4. Fix the error.
  5. What change did you make?


    I changed the == operator to the = operator (i.e., the boolean equals operator to the assignment operator).
  6. Compile PostfixCalculator.java.
  7. How many errors were generated by the compiler?


    10
  8. Why did the number of errors increase?


    The "original" error made it impossible for the compiler to detect any of the others.
  9. Scroll up to the first error. It should be:

    PostfixCalculator.java:29: cannot find symbol
    symbol  : class booleaN
    location: class PostfixCalculator
           booleaN    ok;
           ^
    

  10. Why doesn't the compiler recognize the symbol booleaN?


    Java is case sensitive.
  11. Change booleaN to boolean in line 29.
  12. Compile PostfixCalculator.java.
  13. How many errors were generated by the compiler?


    9
  14. 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);
           ^
    

  15. What line does this error message say is causing the problem?


    38
  16. 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.
  17. How is operator actually spelled?


    opertor
    
  18. Fix this spelling error.
  19. 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.
  20. Compile PostfixCalculator.java.
  21. The first (and hopefully only) error should be:

    PostfixCalculator.java:52: incompatible types
    found   : char
    required: boolean
           else if (operator = '-')
                             ^
    1 error
    

  22. 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.
  23. Change the = operator in line 52 to the == operator (and make sure you understand why).
  24. Compile PostfixCalculator.java.
  25. 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.
  1. Run the application with the command-line parameters 8 3 -.
  2. What was output?


    Infix Expression: 8 - 3
    Result:           11
    
  3. 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);

  4. Run the application with the command-line parameters 8 3 -.
  5. 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);

  6. Run the application with the command-line parameters 8 3 -.
  7. What "debugging output" was generated?


    Operator: -
    
  8. 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)
    
    
  9. Run the application with the command-line parameters 8 3 -.
  10. What "debugging output" was generated?


    Operator: -
    Addition: 8 3
    
  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).
  12. Run the application with the command-line parameters 8 3 -.
  13. Is the output correct?


    Yes.
  14. 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.
  15. Run the application with the command-line parameters 5 7 +.
  16. Is the output correct?


    Yes.
  17. Run the application with the command-line parameters 4 9 *.
  18. What is wrong with the output?


    The infix expression is correct but the result is 0 when it should be 36.
  19. Remove the comment symbols from all of the "debugging output" statements so that they will be executed again.
  20. Run the application with the command-line parameters 4 9 *.
  21. Is the operator correct?


    Yes.
  22. 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.
  23. Fix this mistake.
  24. Run the application with the command-line parameters 4 9 * and 4 9 x to make sure it works for both.
  25. Run the application with the command-line parameters 12 5 /.
  26. Is the output correct?


    Yes.
  27. Run the application with the command-line parameters 12 5 %.
  28. Is the "normal output" correct?


    No. The operator is correct but the expression isn't. In addition, it says the operator is invalid.
  29. The "debugging output" indicates that the remainder block is being executed.
  30. 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.
  31. Fix these mistakes.
  32. Run the application with the command-line parameters 6 3 $.
  33. What is wrong with the output?


    It should contain the message "Invalid Operator!" since the $ is not an operator that the program understands.
  34. 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.)
  35. What code did you add?


    else
    {
        ok = false;
    }
    
  36. "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.
  1. Run the application with the command-line parameters 6 3 +.
  2. What was output?


    Infix Expression: 6 + 3
    Result:           9
    
  3. Run the application with the command-line parameters 6 3 $.
  4. What was output?


    Infix Expression: 3 $ 3
    Invalid Operator!
    
  5. 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.
  6. Fix the error. (If you can't figure it out, ask for help before proceeding.)
  7. What change(s) did you make?


    I switched the first rightOperand to leftOperand in the call to println().
  8. 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.)
  9. 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.
  10. 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.

Copyright 2013