CS 1713-001 Introduction to Computer Science
Spring 2001 -- Exam 2 Answers

  1. Here is one answer (boldface is what is needed for an answer).
    // NumTest.java:  Used for question 1 on Exam 2
    public class NumTest {
    
       public static void main (String[] args) {
          // Exam 2 part 1.a. 
          int[] nums = {23, 40, 15, 37, 19, 63};
          printArray("Part 1.a.", nums);
          // Exam 2 part 1.b. 
          int numsSum = 0; 
          for (int i = 0; i < nums.length; i++)
             numsSum = numsSum + nums[i];
          System.out.println("Part 1.b.:  " + numsSum);
          // Exam 2 part 1.c.
          int smallest = nums[0];  
          for (int i = 1; i < nums.length; i++)
             if (nums[i] < smallest) smallest = nums[i];
          System.out.println("Part 1.c.:  Smallest num: " + smallest);
          // Exam 2 part 1.d.  
          Sorts.insertionSort(nums);
          printArray("Part 1.d.", nums); 
          // Exam 2 part 2.a.  
          System.out.println("Part 2.a.:  Search in nums for 37:  " +
              linearSearch(nums, 37)); 
          // Exam 2 part 2.b.  
          System.out.println("Part 2.b.:  Check for any zeros:  " +
              noZeros(nums)); 
       }
       private static void printArray(String heading, int[] g) {
          System.out.print(heading + ":  Array nums: ");
          for (int i = 0; i < g.length; i++)
             System.out.print(g[i] + " ");
          System.out.println();
       }
       public static int linearSearch(int [] a, int value) {
          for (int i = 0; i < a.length; i++)
             if (a[i] == value) return i;
          return -1;
       }
       public static boolean noZeros(int[] a) {
          for (int i = 0; i < a.length; i++)
             if (a[i] == 0) return false;
          return true;
       }
    }
    
    Finally, here is sample output, for a number of runs, showing one time when money was won:
    Part 1.a.:  Array nums: 23 40 15 37 19 63 
    Part 1.b.:  197
    Part 1.c.:  Smallest num: 15
    Part 1.d.:  Array nums: 15 19 23 37 40 63
    Part 2.a.:  Search in nums for 37:  3
    Part 2.b.:  Check for any zeros:  true
    
    In addition, a student in the course (Rogers) had the following interesting solution to noZeros, which uses linearSearch to search for a zero:
    
       public static boolean noZeros(int[] a) {
          if (linearSearch(a, 0) == -1) return true;
          else return false;
       }
    

  2. Answer included in 1 above.
  3. Here is one answer (boldface is what is needed for an answer).
    // StringTest.java: Used for Question 3 of Exam 2 
    public class StringTest {
    
       public static void main (String[] args) {
          // Exam 2 part 3.a. 
          String[] animals = {"lion", "tiger", "bear",
                          "goat", "horse"};
          printArray("Part 3.a.", animals);
          // Exam 2 part 3.b.  
          System.out.print("Part 3.b.:  Positions 0 and 3 are ");
          if (animals[0].equals(animals[3]))
             System.out.println("equal");
          else
             System.out.println("not equal");
          // Exam 2 part 3.c.
          String last = animals[0];  
          for (int i = 1; i < animals.length; i++)
             if (last.compareTo(animals[i]) < 0) last = animals[i];
          System.out.println("Part 3.c.:  Last animal: " + last);
       }
       private static void printArray(String heading, String[] f) {
          System.out.print(heading + ":  Array fruits: ");
          for (int i = 0; i < f.length; i++)
             System.out.print(f[i] + " ");
          System.out.println();
       }
    }
    
    Here is sample output:
    Part 3.a.:  Array fruits: lion tiger bear goat horse 
    Part 3.b.:  Positions 0 and 3 are not equal
    Part 3.c.:  Last animal: tiger
    
    Finally, here is an interesting variation of Part 3.c due to a student (George):
          // Exam 2 part 3.c.
          int index = 0;  
          for (int n = 1; n < animals.length; n++)
             if (animals[n].compareTo(animals[index]) > 0) index = n;
          System.out.println("Part 3.c.:  Last animal: " + animals[index]);
    

  4. Here is one answer (boldface is what is needed for an answer).
    // ChuckaLuck.java: play the game Chuck-a-Luck 
    public class ChuckaLuck {
       public static final int RUNS = 1000;
    
       public static void main (String[] args) {
          // create an instance of the Dice class
          Dice dice = new Dice();
          // create an array of four counters
          int[] counters = new int[4];
          // create an array to hold three rolls
          int[] rolls = new int[3];
          int numFours; // count the number of fours
          // play Chuck-a-Luck RUNS many times
          for (int i = 0; i < RUNS; i++) {
             numFours = 0;
             // roll the three dice
             for (int j = 0; j < 3; j++) {
                rolls[j] = dice.roll(); // don't really need to record rolls
                if (rolls[j] == 4) numFours++;
             }
             // count the results
             counters[numFours]++;
          }
          int totalMoney; // money won or lost
          totalMoney = counters[0]*(-1) + counters[1]*1 +
             counters[2]*2 + counters[3]*3;
          for (int i = 0; i < 4; i++)
             System.out.println(i + " Four(s) occured " + counters[i] + " times");
          System.out.println("Total Money bet: $" + RUNS);
          System.out.print("Total Money (won/lost): $" + Math.abs(totalMoney));
          if (totalMoney < 0) System.out.println(" lost");
          else System.out.println(" won");
          System.out.println("Percent Loss: " + (double)totalMoney/RUNS);
       }
    }
    
    Also need a Dice class. Here is a minimal version:

    // Dice.java: roll a single die
    public class Dice {
       int roll() {
          return (int)(6.0*Math.random() + 1.0);
       }
    }
    
    Finally, here is sample output, for a number of runs, showing one time when money was won:
    0 Four(s) occured 589 times
    1 Four(s) occured 330 times
    2 Four(s) occured 73 times
    3 Four(s) occured 8 times
    Total Money bet: $1000
    Total Money (won/lost): $89 lost
    Percent Loss: -0.089
    =================================
    0 Four(s) occured 583 times
    1 Four(s) occured 348 times
    2 Four(s) occured 63 times
    3 Four(s) occured 6 times
    Total Money bet: $1000
    Total Money (won/lost): $91 lost
    Percent Loss: -0.091
    =================================
    0 Four(s) occured 576 times
    1 Four(s) occured 343 times
    2 Four(s) occured 77 times
    3 Four(s) occured 4 times
    Total Money bet: $1000
    Total Money (won/lost): $67 lost
    Percent Loss: -0.067
    =================================
    0 Four(s) occured 543 times
    1 Four(s) occured 372 times
    2 Four(s) occured 81 times
    3 Four(s) occured 4 times
    Total Money bet: $1000
    Total Money (won/lost): $3 won
    Percent Loss: 0.0030
    
    Here is a larger run with RUNS = 100000000
    0 Four(s) occured 57859724 times
    1 Four(s) occured 34726726 times
    2 Four(s) occured 6949995 times
    3 Four(s) occured 463555 times
    Total Money bet: $100000000
    Total Money (won/lost): $7842343 lost
    Percent Loss: -0.07842343