JMU
Starts and Completions
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
The Naive Approach to Completions
The Obvious Fix for Completions
The Pattern for Completions
javaexamples/programmingpatterns/StartsAndCompletions.java (Fragment: completions)
    /**
     * 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;
    }
        
The Naive Approach to Starts
The Obvious Fix for Starts
The 10,000 Monkeys Fix for Starts
The Pattern for Starts
The Remainder Indicator
javaexamples/programmingpatterns/StartsAndCompletions.java (Fragment: indicator)
    /**
     * 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;
        }
    }
        
Starts
javaexamples/programmingpatterns/StartsAndCompletions.java (Fragment: starts)
    /**
     * 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);
    }
        
Examples