|
Starts and Completions
A Programming Pattern |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
/**
* Determine the number of completed tasks from
* an amount of work and a numeraire.
*
* @param work The amount of work
* @param workPerTask The work per task
* @return The number of completed tasks
*/
public static int completions(int work, int workPerTask) {
return work / workPerTask;
}
/**
* Returns 0 if numerator is evenly divisible by denominator
* and 1 otherwise. That is, returns 1 if there is a remainder
* and 0 otherwise.
*
* @param num The numerator
* @param den The denominator
* @return 1 if there is a remainder; 0 otherwise
*/
public static int remainderIndicator(int num, int den) {
if ((num % den) == 0) {
return 0;
} else {
return 1;
}
}
/**
* Determine the number of started tasks from
* an amount of work and a numeraire.
*
* @param work The amount of work
* @param workPerTask The work per task
* @return The number of started tasks
*/
public static int starts(int work, int workPerTask) {
return completions(work, workPerTask)
+ remainderIndicator(work, workPerTask);
}
26 / 3 or 8 laps, but
started 9 (since 26 % 3 is non-zero)
9 / 9 or 1 game, and also
started 1 game (since 9 % 9 is
zero)