Consider one more time the following contract specification for the static metho
ID: 668009 • Letter: C
Question
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.
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.
Provide two distinct implementations of the newly designed smooth method, one recursive and one iterative (i.e., not using recursion). Use only the SequenceKernel methods (add, remove, and length, plus the methods listed there as inherited from Standard).
Explanation / Answer
java method:
public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {
assert s1 != null : "Violation of: s1 is not null";
assert s2 != null : "Violation of: s2 is not null";
assert s1.length() >= 1 : "|s1| >= 1";
s2.clear();
if (s1.length() > 1) {
int j = 1;
for (int i = 0; i < (s1.length() - 1); i++) {
int> int two = s1.remove(j - 1);
int / 2;
int twoT = two / 2;
int temp = oneT + twoT;
if ((one % 2 != 0 || two % 2 != 0) && (one > 0 && two > 0)) {
temp++;
}
if ((one % 2 != 0 || two % 2 != 0) && (one < 0 && two < 0)) {
temp--;
}
s2.add(i, temp);
s1.add(i, one);
s1.add(j, two);
j++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.