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

Laboratory Exercise: Local and Global Variables;
Constructor Parameters and Static Variables

Quick Index


One general principle of object-oriented design states:

Applying this principle to the simulation of Lab 2, we note that the design of Alternative 1 uses only two special fields within an object: one to specify whether the program will specify racquetball or volleyball, and the other to specify the number of games to be simulated. A third field, winByTwo, is computed based on the specific game. However, the design does not require special data structures or interactions among classes. For example, simulateGames is self contained, except for needing to know the number of games for a simulation; and playUntilWin is self contained, except for needing to know whether the server must win by 2 points.

Without a need for arrays or other special structures, the various methods for this simulation program seem largely to be independent and self contained; that is, they do not rely much upon an associated object. From this perspective, the above general principle suggests that the fields outside individual methods and the methods themselves should be static. Running a simulation does not depend upon individual objects and object interactions.

Further, a user could edit the static variables to specify relevant details:

    // Edit these lines to indicate the game to be simulated 
    // and the number of games to simulate 
    public static String game = "racquetball";
   	// "racquetball" or "volleyball" 
   public static int numberOfGames = 1000;

This alternative has several consequences:

  1. Methods simulateGames and main can be combined, since no objects must be declared, initialized, and used.
  2. Field winByTwo can be set within main by checking the game field.
  3. No constructors are needed, since there are no fields or methods tied to objects.

Program implements this Alternative Solution 2, using the recursive Game Approach 1. The result is a streamlined program (128 lines total, in contrast to the 154 lines required for recursive implementation for Alternative 1 - about a 16.9% reduction in code).

Steps for this Lab

  1. Review both and

    1. What are the main advantages of each Alternative Solution?
    2. To what extent does the general principle, stated at the start of this lab, apply for this simulation? (The simulation uses three variables outside individual methods, but the simulation does not require special data structures.)
    3. Which alternative do you prefer? Briefly justify your conclusion.
  2. Revise the three iterative solutions (programs,, and to make all class-level fields and methods static . Simplify the resulting code as much as possible.