Software Development, Design Choices, Class Design,
Recursion and Iteration, Testing, and Command-line Arguments
2011-2012
A Racquetball or Volleyball Simulation
 
 

Laboratory Exercise: Class-level Boolean Variables for Tracing

Quick Index

Discussion

The results from the sample output illustrate that this simulation is very sensitive to the initial probabilities. For example, if A has a 40% chance of winning a volley, then A will win almost no games. Since these results may not be intuitive, it is natural to wonder how this simulation might be tested.

Although eventually a user will want to view only the results of the simulation, a developer may want to know how processing proceeds at a reasonable level of detail. To obtain such information, it is natural to add various print statements to show intermediate values. One way to proceed would be to insert these print statements for testing and then remove them when the program is believed to be correct and complete. However, this approach has at least two drawbacks:

  1. Even after a program seems done, later questions may arise that motivate the re-insertion of print statements.
  2. Removal of print statements for tracing may introduce new errors.

Instead, in order to trace code when desired, a static variable printGame can be added to the class. When this variable is true, the score of each game is printed, and when this variable is false, the scores for the individual games are not printed. This approach is illustrated in program Game3Recur.java.

Steps for this Lab

  1. Run Game3Recur.java with printGame being true and again with the variable false. Explain why this change results in any differences in what is printed.

  2. Describe the extent to which having printGame helps you check the program for correctness.

  3. To refine tracing capabilities, two different levels of tracing might be defined:

    • Game-level tracing: print the results of each game in the simulation.
    • Volley-level tracing: print the results of each volley, with who won and the current score for each team.

    Revise Game3Recur.java to implement these two tracing levels. You will need to add at least one additional tracing variable, and you will need to organize print statements according to the type of tracing they support. Additional print statements also may be needed.

  4. What other tracing might be helpful to establish the simulation is correct?

  5. Revise the three iterative solutions (programs Game1Iter1.java, Game1Iter2.java, and Game1Iter2Alt.java) to add a tracing capability.

  6. Consider the following commentary:

    Technical Aside: The sample output shown in the first lab may be explained mathematically by noting that, to score a point, a team must win two volleys in a row. Thus, if A's chance of winning a volley is 0.4, A's chance of winning a point is 0.4x0.4 = 0.16. B's chance of winning a point in this case is 0.6x0.6 = 0.36, which is more than double A's chance of scoring a single point.

    To what extent does this observation justify the output of this simulation?