Software Development, Design Choices, Class Design, Recursion and Iteration, Testing, and Command-line Arguments |
2011-2012 | |
A Racquetball or Volleyball Simulation | ||
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:
Program Game2Recur.java 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).
Review both Game1Recur.java and Game2Recur.java.
Revise the three iterative solutions (programs Game1Iter1.java, Game1Iter2.java, and Game1Iter2Alt.java) to make all class-level fields and methods static . Simplify the resulting code as much as possible.