Solutions to final exam review questions ** Question 1 ************************************************************** /** Java version of the transposeNegative question */ class RQ2 { /** = matrix where negative pairs across the main diagonal in m * are transposed. Assume m is square. */ public static int[][] transposeNegative(int[][] m) { int n = m.length; //dimension of square m //iterate through upper triangular part of m for (int x= 0; x < n; x++) for (int y= x + 1; y < n; y++) if (m[x][y] < 0 && m[y][x] < 0) { //swap when both are negative int temp = m[x][y]; m[x][y] = m[y][x]; m[y][x] = temp; } return m; } } /** Java version of the random walk question. OOP design is used--see * class Point below. * You can solve this question without OOP, in which case the solution * would be very similar to the MATLAB solution. */ public class RQ3 { /** = random int between min and max inclusive */ public static int randInt(int min, int max) { return min + ((int)(Math.random() * (max - min + 1))); } /** Update position */ public static void move(Point p) { int i = randInt(0,3); if (i==0) p.x++; else if (i==1) p.x--; else if (i==2) p.y++; else if (i==3) p.y--; } /** Simulate the random walk */ public static void simulate(int steps) { Point p = new Point(randInt(-10, 10), randInt(-10, 10)); for (int i = 1; i <= steps; i++) { move(p); System.out.println("Step " + i + " Position: x = " + p.x + " y = " + p.y); } } public static void main(String []args) { System.out.print("Enter the number of steps:"); simulate(JLiveRead.readLineInt()); } } /** x- and y-coordinates of a Point */ class Point { public int x, y; Point(int x1, int y1) { x = x1; y = y1; } } ** Question 2 ************************************************************** 3 7 15 ** Question 3 ************************************************************** /** Create 4 Creatures and have each act 5 times */ public class Zoo { public static void main(String[] args) { int times = 5; // number of times to act Creature[] c = { new Dog("Ginger"), new Cat("Macavity",3), new Cat("Gus",7), new DogCow() }; for (; times>0; times--) for (int i = 0; i=2 // constructor: assign name, purrLength public Cat(String name, int purr) { super(name); purrLength = purr; } // speak: meow public void speak() { System.out.println(name + " meows"); } // purr public void purr() { System.out.print(name + " purr"); for (int i = 3; i<=purrLength; i++) System.out.print("r"); System.out.println("s"); } // act: randomly choose between purr and super.act public void act() { if (Math.random() < .5) purr(); else super.act(); } } //class Cat public class Dog extends Creature { private int wags = 0; // number of wags already performed // constructor: name is name public Dog(String name) { super(name); } // speak: woof public void speak() { System.out.println(name + " woofs"); } // wag and print number of times wagged public void wag() { wags++; System.out.println(name + " wags its tail for the " + wags + "-th time"); } // act: randomly choose between wag and old choice for act public void act() { if (Math.random() < .5) wag(); else super.act(); } } //class Dog public class DogCow extends Dog { // constructor: DogCows are always named "Bessy" public DogCow() { super("Bessy"); } // speak: randomly choose between moof and super.speak public void speak() { if (Math.random() < .5) super.speak(); else System.out.println(name + " moofs"); } } //class DogCow