Spring 2001 CS100M Project P6: Matlab Masquerade due 6pm, Friday, April 13 =============================================================================== Submission -- all parts are to be submitted online =============================================================================== Formatting and style requirements: + include a helpful and correct invariant for each loop + no tabs! uses spaces instead. + use and assume a fixed-width font, e.g. courier + at most 80 characters per line + [4/08] in comments, denote exponentiation with $**$ instead of $^$ because $^$ has a different meaning in Java than it does in Matlab Special coding requirements: + do not use $for$ loops at all + do not use object-oriented programming techniques (we probably won't see them before Thursday, April 12, anyway) Deadline: 6pm, Friday, April 13 =============================================================================== P6.1: debug Java code for P4.1 (rightmost longest arith. input sequence) =============================================================================== + note: the code in E12 is only supposed to work for integer input values -- it is not a bug that it does not work for floating point values + find at least one input sequence for which the code produces the wrong output. include all the output (prompt, input values, program output) from running the program on that input sequence inside a comment in the code. + fix the code so that it is correct. submit the revised code, including the comment containing the erroneous output of the original code. remember to include identifying info about the assignment, yourself, and your partner (if you have one). =============================================================================== P6.2: write Java code for P5.2 (Condorcet voting, string processing, 2D arrays) =============================================================================== + modify methods $condorcet$, $tallies$, $tally$ in the provided skeleton. you may define additional methods, but it is possible to write clear, concise, non-redundant code without doing so. + note: we do not ask for $alldefeats$ since sorting is not as simple in Java as it is in Matlab + note: do not do SSD Condorcet voting + helpful examples --Java versions of R1.6, R2.2-- are available from the Projects webpage. =============================================================================== P6.3: write Java code for T1.3 (series) =============================================================================== N write class $P6_3$ with: ----- k (2 k + 1) + a function $f(x,N)$ that computes: \ (-1) x + a main method that prints $f(2,10)$ ) ---------------- + do not use any arrays / (2 k + 1)! hints: ----- + look at the sample T1.3 solutions k = 0 + look at the Matlab and Java code below warning: code below has not been tested yet (but even if it has errors, it should still provide a helpful idea of what to do) % Matlab function "solution" to P2.2 that uses no arrays that uses no arrays function total = p2_2(N,x) % total = p2_2(N,x): return sum of 1st $N$ terms of Taylor series for $exp(x)$ % example: $p2_2(150,2)$ total = 0.0; % sum so far of terms i = 0; % number of terms summed so far factorial = 1.0; % factorial of $i$ pow = 1.0; % $x ** i$ % compute sum of first $N$ terms of taylor series for $exp(x)$ % inv: maintain variable definitions above while i < N total = total + pow / fact; pow = pow * x; i = i + 1; factorial = i * factorial; end // Java version of Matlab code above ($args$ doesn't count as using an array) // with a main method containing a sample call $p2_2(150,2)$ public class P2_2 { public static void main(String[] args) { int N = 150; double x = 2.0; System.out.println("p2_2(" + N + "," + x + ") = " + p2_2(N,x)); } // sum of 1st $N$ terms of Taylor series for $exp(x)$ public static double p2_2(int N, double x) { double total = 0.0; // sum so far of terms int i = 0; // number of terms summed so far double factorial = 1.0; // factorial of $i$ double pow = 1.0; // $x ** i$ // compute sum of first $N$ terms of taylor series for $e ** x$ // inv: maintain variable definitions above while (i < N) { total = total + pow / fact; pow = pow * x; i = i + 1; factorial = i * factorial; } return total; // [4/09] added this line } } =============================================================================== P6.4: write Java code for T2.4 (2nd-to-last mode of pre-sorted input sequence) =============================================================================== translate the sample solution to T2.4 into a Java class $P6_4$ with a main method that reads a pre-sorted, 100-terminated input sequence and prints the second-to-last mode. + do not use any arrays. + note: don't try to use something like $struct$s or define objects + tip: look at the code for P6.1 to see how to do input =============================================================================== P6.5: write Java code for P2.4 (bacteria, small life, Fibonacci, 1D arrays) =============================================================================== write a class $P6_5$ with a main method performs the task specified for P2.4. + use good style, e.g. make the program easy to modify. + define a function $advance(ages,hours)$ that takes a reference $ages$ to an integer array of ages of bacteria and a non-negative integer $hours$ of hours and returns a reference to an integer array. + use $return$ to return either: + core: the $null$ pointer [4/10] that is, the function does not compute anything -- $main$ does all the computation. + bonus: a reference to a new array of the ages of the resulting bacteria after $hours$ hours elapse. [4/10] that is, the function computes the new list of ages -- $main$ must call this function instead of doing the computation itself. [4/11] the function should never create any bacteria of age 0.