Homework: Sequence Smooth as a Function


Consider one more time the following contract specification for the static method smooth.

    /**
     * Smooths a given {@code Sequence<Integer>}.
     * 
     * @param s1
     *            the sequence to smooth
     * @param s2
     *            the resulting sequence 
     * 
     * @replaces s2
     * @requires |s1| >= 1
     * @ensures <pre>
     * |s2| = |s1| - 1  and
     *  for all i, j: integer, a, b: string of integer
     *      where (s1 = a * <i> * <j> * b)
     *    (there exists c, d: string of integer
     *       (|c| = |a|  and
     *        s2 = c * <(i+j)/2> * d))
     * </pre>
     */
    public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

Answer the following questions.

  1. Redesign the method so that it is a function that returns the new (smoothed) sequence instead of replacing a parameter. You need to modify the method header and update the formal contract to reflect the changes.
  2. Provide two distinct implementations of the newly designed smooth method, one recursive and one iterative (i.e., not using recursion). While you may use method entry, do not use any other method that is introduced in the enhanced interface Sequence. Among the methods still permitted for your use are all those inherited by or introduced in SequenceKernel, including add, remove, and length.