JMU JMU - Department of Computer Science
Help Tools
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:

  1. Depending on your development environment, create either a directory or a project for this lab.
  2. Setup your development environment.
  3. 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.
  1. Read and understand InfixCalculator.java, but don't change any errors you might find.
  2. Compile InfixCalculator.java.
  3. What error message was generated by the compiler?


    InfixCalculator.java:42: not a statement
           ok     == true;
                  ^
    1 error
    
    Expand
  4. What line in InfixCalculator.java contains the error?


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


    I changed the == operator to the = operator (i.e., the relational equals operator to the assignment operator).
    Expand
  7. Compile InfixCalculator.java.
  8. How many errors were generated by the compiler?


    8
    Expand
  9. Why did the number of errors increase?


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

    InfixCalculator.java:25: cannot find symbol
    symbol  : class booleaN
    location: class InfixCalculator
           booleaN    ok;
           ^
    

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


    Java is case sensitive.
    Expand
  12. Change booleaN to boolean in line 25.
  13. Compile InfixCalculator.java.
  14. How many errors were generated by the compiler?


    7
    Expand
  15. 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
    

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


    36
    Expand
  17. 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.
  18. How is the variable spelled in the declaration?


    opertor
    
    Expand
  19. Fix this "spelling error".
  20. 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.
    Expand
  21. Compile InfixCalculator.java.
  22. The first error should be:

    InfixCalculator.java:36: error: incompatible types: String cannot be converted t
    o char
           operator = JMUConsole.readLine();
                                         ^

  23. What does this error mean?


    The readLine() method returns a String but operator is declared to be a char.
    Expand
  24. 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.
    Expand
  25. 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.

    Expand
  26. Change the call to readLine() to a call to readLineAsChar().
  27. What code did you add?


            operator = JMUConsole.readLineAsChar();
    
    Expand
  28. Compile InfixCalculator.java.
  29. The first (and hopefully only) error should be:

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

  30. 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.
    Expand
  31. Change the = operator in line 52 to the == operator (and make sure you understand why).
  32. Compile InfixCalculator.java.
  33. 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.
    Expand
  34. What file was created (now that the InfixCalculator class can be compiled)?


    InfixCalculator.class
    Expand
  35. What does this file contain?


    The byte code (which is almost machine instructions) for the human-readable source code in InfixCalculator.java.
    Expand
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 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.)
  2. What was output?


    Result: 11
    
    Expand
  3. 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);

  4. Run the application with the inputs 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.

    JMUConsole.printf("Operator: %c\n", operator);

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


    Operator: -
    
    Expand
  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, 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.

  9. Run the application with the input 8 - 3.
  10. What "debugging output" was generated?


    Operator: -
    Addition
    Result: 11
    
    Expand
  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 input 8 - 3.
  13. Is the output correct?


    Yes.
    Expand
  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 input5 + 7.
  16. Is the output correct?


    Yes.
    Expand
  17. Run the application with the input 4 * 9.
  18. What is wrong with the output?


    The result is 0 when it should be 36.
    Expand
  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 input 4 * 9.
  21. Is the operator correct?


    Yes.
    Expand
  22. Why isn't the multiplication block executed?


    The && operator should be the || operator.
    Expand
  23. Fix this mistake.
  24. Run the application with the inputs 4 * 9 and 4 x 9 to make sure it works for both.
  25. Run the application with the inputs 12 / 5.
  26. Is the output correct?


    Yes.
    Expand
  27. Run the application with the input 12 % 5.
  28. Is the output correct?


    No. The operator is correct but there is no result.
    Expand
  29. 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.
    Expand
  30. Fix these mistakes.
  31. Run the application with the input 12 % 5.
  32. Is the output correct?


    Yes.
    Expand
  33. Run the application with the input 6 $ 3.
  34. What is wrong with the output?


    It should contain the message "Invalid Operator!" since the $ is not an operator that the program understands.
    Expand
  35. Fix this mistake by adding an else block. Do not add any "output" statements.
  36. What code did you add?


    else
    {
        ok = false;
    }
    
    Expand
  37. Run the application with the input 6 $ 3.
  38. Is the output correct?


    Yes.
    Expand
  39. "Comment out" the debugging output by putting a // in front of each.

Copyright 2019