public class R2 { // run code on sample test cases public static void main(String[] args) { // R2.1 double[][] jdk = new double[][] { new double[] { 1.0, 1.0, 9.5 }, new double[] { 1.0, 2.0, 9.5 }, new double[] { 1.0, -2.0, 9.5 }, new double[] { 10.0, -1.5, 3.3 }, new double[] { 10.0, 1.5, 3.3 }, new double[] { 10.0, 0.0, 3.3 }, new double[] { 10.0, 1.0, 10.0 } }; int i = 0; while (i < jdk.length) { System.out.print("mycolon(" + jdk[i][0]+"," + jdk[i][1]+"," + jdk[i][2]+") = "); double[] d = mycolon(jdk[i][0], jdk[i][1], jdk[i][2]); // print contents of $d$ int j = 0; while (j < d.length) { System.out.print(" " + d[j]); j += 1; } System.out.println(); i += 1; } // R2.3 int x = 3; int a = 4; int b = 7; System.out.println("x = " + x + "; a = " + a + "; b = " + b); a = g(b+2, x*5); System.out.println("x = " + x + "; a = " + a + "; b = " + b); // R2.4 double[] xx = new double[5]; double[] yy = new double[xx.length]; i = 0; while (i < xx.length) { xx[i] = i * 60 + 50; yy[i] = Math.random() * 200+50; i += 1; } SpringWindow d = new SpringWindow(xx,yy,0.6,.1,100); d.resize(400,300); d.move(0,75); d.setTitle("Spring"); d.show(); d.toFront(); // R2.5a dohist(new int[] { 1, 5, 5, 7, 7, 7, 8, 8, 9, 1000 }); dohist(new int[] { 1, 5, 5, 7, 7, 7, 8, 8, 9, 10, 10, 10 }); // R2.6: matlab-specific } // R2.1 ====================================================================== // return array corresponding to matlab's $j:d:k$ public static double[] mycolon(double j, double d, double k) { if (d == 0) { return new double[0]; } int len = 1 + (int) (Math.floor((k - j) / d)); if (len < 0) { len = 0; } double[] i = new double[len]; int x = 0; // inv: $i[0..x-1]$ has correct values while ((d > 0.0 & j <= k) | (d < 0.0 & j >= k)) { i[x] = j; x += 1; j += d; } return i; } /* matlab version: function i = mycolon(j,d,k) % i = mycolon(j,d,k): return $i = j:d:k$ i = []; if d == 0 return; end len = floor((k - j) / d); len = max(0, 1+len); i = zeros(1,len); x = 1; % inv: $i(1:x-1)$ has correct values while (d > 0 & j <= k) | (d < 0 & j >= k) i(x) = j; x = x + 1; j = j + d; end */ // R2.2: see links for P6 on Projects webpage // R2.3 ====================================================================== // tracing question -- basically, as shown in lecture for matlab; // here is how to define the functions in java public static int f(int a, int b) { int x = a + b; if (a >= b & b > 0) { x = f(a-b, b); } else if (a > 0) { x = g(b, a); } return x; } public static int g(int a, int b) { if (a > b & b > 0) { return g(a-b, b); } else { return f(b, a); } } /* matlab: function x = f(a,b) x = a+b; if a >= b & b > 0 x = f(a-b, b); elseif a > 0 x = g(b,a); end function x = g(a,b) if a > b & b > 0 x = g(a-b, b); else x = f(b, a); end */ // R2.4 ====================================================================== // code is at bottom of the file // R2.5a ===================================================================== // Given vector $v$ of non-negative integers pre-sorted in ascending order, // read numbers from $v$ until 1000 appears or run out of elements; print // a horizontal histogram of the numbers (each "*" = 1 occurrence) // note: formatting isn't perfect -- we'll do formatted output later public static void dohist(int[] v) { TokenReader in = new TokenReader(System.in); final int stopping = 1000; // stopping value int i = 0; // position of next element of $v$ to process int prev = 0; // previous number, for which all output has been produced, // except for newline // note: no need for $stop$ because $&&$ is short-circuiting, i.e. // if $i < v.length$ is false, then $v[i] != stopping$ is not executed // inv: maintain definitions above; remaining work to be done is for v[i..] while (i < v.length && v[i] != stopping) { // print gaps and, if needed, label for $v[i]$ // inv: still need to print labels for $prev+1:v[i]$ while (prev != v[i]) { prev = prev + 1; System.out.print("\n" + prev + " "); } System.out.print('*'); // print $'*'$ for $v[i]$ i = i+1; } System.out.println(); } /* matlab: % Given vector $v$ of non-negative integers pre-sorted in ascending order, % read numbers from $v$ until 1000 appears or run out of elements; print % a horizontal histogram of the numbers (each "*" = 1 occurrence) stopping = 1000; % stopping value i = 1; % position of next element of $v$ to process prev = 0; % previous number, for which all output has been produced, % except for newline stop = 0; % == "seen $stopping$ in $v$ yet?" % inv: maintain definitions above; remaining work to be done is for v(i:end) while ~stop & i <= length(v) % [3/14] fixed: $<=$ was wrongly $<$ if v(i) == stopping stop = 1; else % print gaps and, if needed, label for $v(i)$ % inv: still need to print labels for $prev+1:v(i)$ while prev ~= v(i) prev = prev + 1; fprintf('\n%3d ', prev); end fprintf('*'); % print $'*'$ for $v(i)$ i = i+1; end end fprintf('\n') */ // R2.6 ====================================================================== // omitted: matlab-specific } // R2.4 code ================================================================ // some of this might not make sense until next week import java.awt.*; // for graphics // Class Drawing: a simple graphics window. public class SpringWindow extends Frame { double[] x, y; // initial position of balls double k; // spring constant double dt; // size of time step int T; // number of time steps to simulate public SpringWindow(double[] x, double[] y, double k, double dt, int T) { this.x = x; this.y = y; this.k = k; this.dt = dt; this.T = T; } // draw spring for $T$ time steps public void paint(Graphics g) { g.setColor(Color.black); int n = x.length; // n = length(x); double[] x = (double[]) (this.x.clone()); // copy $this.x$ double[] y = (double[]) (this.y.clone()); // copy $this.y$ double[] vx = new double[n]; // vx = zeros(1,n); double[] vy = new double[n]; // vy = zeros(1,n); int t = T; while (t >= 0) { t -= 1; int i = 1; // plot(x,y) while (i < x.length) { g.drawLine((int) x[i-1], (int) y[i-1], (int) x[i], (int) y[i]); int r = 3; g.fillOval((int) x[i-1]-r, (int) y[i-1]-r, 2*r+1, 2*r+1); i += 1; } // compute accelerations (ax(i),ay(i)) for all balls i double[] ax = new double[n]; // ax = zeros(1,n); double[] ay = new double[n]; // ay = ax; i = 0; while (i < n) { // for i = 1:n if (i>0) { // if i > 1 ax[i] = ax[i] + x[i-1] - x[i]; ay[i] = ay[i] + y[i-1] - y[i]; } if (i