Solutions to Prleim 3 review questions ** Question 1 ************************************************************** /** = return 1-d array of row sums of 2-d array a */ public static double[] rowSumsOf2dArray(double[][] a) { double[] sums = new double[a.length]; // 1-d array to store row sums for (int r=0; r=1/3 represents heads. */ public class CoinGame { //Simulate the coin game of Review Q5 //Procedural solution public static void main(String[] args) { int trials= 1000; //no. of games int nBwin= 0; //no. of times B has won so far double[] toss= new double[4]; //results of the coin tosses //toss[i]<1/3 means tail //toss[i]>=1/3 means head //toss[0..1] are A's tosses //toss[2..3] are B's tosses for (int i=0; i=1/3.0) || (toss[2]>=1/3.0 && toss[3]<1/3.0) )) nBwin++; } System.out.println("Player B wins " + nBwin + " times."); } } /** This solution uses integer random numbers. An extra method is * written to generate the int and classify it as heads or tails */ public class Q5{ public static void main(String[] args) { int counter1 = 0; //counter number of time B wins for (int i = 0; i < 1000; i++) { //A rolls twice, variable a is true if same face come up, //false otherwise boolean a = (roll() == roll()); //B rolls twice, variable b is true if same face come up, //false otherwise boolean b = (roll() == roll()); //If A have different face, B have different face, then B wins //increment the counter if ((a == false) && (b == false)) counter1++; } System.out.println(counter1); } //roll method, //if result is Head return 0, tail return 1 public static int roll() { int r = (int)(Math.random()*3); if (r == 2) r = 0; return r; } }