Here is a draft of solutions to Part 1 of the review questions. _______________________________________________________________________________ 1) // return the Java equivalent of the Matlab expression $a(i,j)$ static int[][] index(int[][] a, int[] i, int[] j) { // set b[row][col] to a[i[row]][j[col]] and return b int[][] b = new int[i.length][j.length]; for (int row = 0; row= x2$ be the values in $x$. Let $y1 >= y2$ be the values in $y$. [y1,y2 are shorter names than ymax,min] Assume $x=[x1 x2]$ is sorted, as allowed by Part (a). Then $y=[y1 y2]$ is sorted with distance $|x1-y1| + |x2-y2|$, and $y=[y2 y1]$ is unsorted with distance $|x1-y2| + |x2-y1|$. We wish to prove that the sorted distance is at most the unsorted distance. Let us proceed by case analysis on Cases 0-to-3. Case 0: y1 > x1 Exchange $x$ and $y$ and continue with the appropriate case below. Case 1: x1 >= y1 >= y2 >= x2 x1 y1 y2 x2 picture: +-------+-------+-------+ sorted distance: +-------+ +-------+ shorter by 2|y1-y2| unsorted distance: +-------+-------+ +-------+-------+ Case 2: x1 >= y1 >= x2 >= y2 x1 y1 x2 y2 picture: +-------+-------+-------+ sorted distance: +-------+ +-------+ shorter by 2|y1-x2| unsorted distance: +-------+-------+-------+ +-------+ Case 3: x1 >= x2 >= y1 >= y2 x1 x2 y1 y2 picture: +-------+-------+-------+ sorted distance: +-------+-------+ same distnce +-------+-------+ unsorted distance: +-------+-------+-------+ +-------+ (c) Part (a) allows us to assume $x$ is sorted. Let $y$ be in any order. Let $ys$ be $y$ in sorted order; note that $ys$ is unique: there is only one sorted order. Part (b) tells us that sorting $y$ by swapping elements that are out of order reduces the distance (or leaves it unchanged). That is, the distance between $y$ and $x$ is greater or equal to the distance between $ys$ and $y$. Therefore, the minimum distance is achieved by $ys$, the sorted rearrangement of $y$. _______________________________________________________________________________ 5) observe that the boundary of the triangle is a diagonal line. this has an equation of the form col = row + constant. we solve for $constant$ by looking at row = 1: the number of elements = min(width,height). therefore, the leftmost column at row=0 is at position // ^^^^^ there was a typo, $1$ for $0$ $col = width-min(width,height) = constant$. // return the sum of the largest possible upper right triangle // in non-empty, row-major, rectangular array a int sumUpperTri(int[][] a) { int height = a.length, width = a[0].length; // dimensions of a // boundary of triangle is given by the equation: col = row + constant int constant = width - Math.min(width,height); // ^^^^^ there was a typo: $=$ for $-$ // set sum to elements in the triangle: // sum includes all appropriate elements from rows above row i, and // all appropriate elements in row i from columns to the left of col j int sum = 0; for (int i = 0; i0 || current>0 ) { // ! IS DISALLOWED previous = current; current = in.readInt(); } System.out.println( previous + " " + current); (b) To print the first good pair of consecutive integers, make the loop guard $previous<=0 && current<= 0$ _______________________________________________________________________________ 11) // test p for primality TokenReader in = new TokenReader(System.in); int p = in.readInt(); int d = 2; while ( p % d != 0) d = d+1 ; // typo fix: the $p == d$ and $p != d$ cases were switched if ( p == d ) System.out.println( p + " is prime"); else System.out.println( p + " is not prime"); _______________________________________________________________________________ 12) Two Persons *bilaterally* interact ("bilateral" means "something done in both directions") by having each Person *unilaterally* interact with the other ("unilateral" means "something done in only one direction"). A *source* Person unilaterally interacts with a *target* Person as follows: + The source Person rolls his/her own die to obtain a number from 0 to his/her own $friendliness$ (inclusive). + If the die comes up 0, then the target Person becomes the latest enemy of the source Person, otherwise the source Person's enemy is unchanged. (a) class Person { private String name; protected int friendliness; public Person enemy; // Constructor public Person(String n, int f) { name = n; friendliness = f; enemy = null; // <-- oops, not specified in original question } // return description (name, friendliness, name of enemy) // (this is like method $describe$ on Prelim 2) public String toString() { String s = " no enemy"; if (enemy != null) s = " enemy " + enemy.name; return name + " friendliness " + friendliness + s; } // bilaterally interact with Person p public void interact(Person p) { uniInteract(p); p.uniInteract(this); } // unilaterally interact with Person p public void uniInteract(Person p) { if (0 == (int)(Math.random()*(1+friendliness))) enemy = p; } } (b) class WackyPerson extends Person { // constructor: set name to n, friendliness to 2000, enemy to self WackyPerson(String n) { super(n,2000); // <-- MUST use $super$, since $name$ is $private$ enemy = this; } // unilaterally interact: do 100 times what a Person would do public void uniInteract(Person q) { for ( int i = 0; i<100; i++) super.uniInteract(q); } } (c) The indices are wrong: p[0] not p[1] for Spike, p[1] not p[2] for Buffy. Spike rolls his die once, Buffy hers 100 times. Person[] p = { new Person("Spike",10), new WackyPerson("Buffy") }; p[1].interact(p[2]); // have Spike and Buffy interact