Computer Science 15-100 (Sections S-V), Fall 2008
Homework 4
Due:  Thu 25-Sep-2008 at 11:59pm (email copy) and at Friday's class/recitation (identical physical copy)
(no late submissions accepted).


Read these instructions first!


  1. codeTracing
  2. diceRolling
    1. de Méré's Problem
    2. Yahtzee!
  3. paintingFlags
    1. Benin
    2. Norway
    3. Maldives
    4. United States
  4. Bonus/Optional:  paintingDominoes

  1. codeTracing
    Using the table-based code tracing technique we covered in class, manually trace each of the following programs.  Your answer is not just what this method prints out, but the table showing the entire trace.
     
    1.  1  class Foo {
       2    public static void main(String[] args) {
       3      int x = 2, y = 12;
       4      while (y-x > y/x) {
       5        x+=7;
       6        y+=3;
       7        System.out.println(x + "," + y);
       8      }
       9      System.out.println(x + "," + y);
      10    }
      11  }
      
    2.  1  class Foo {
       2    public static void main(String[] args) {
       3      int x = 2, y;
       4      for (y=(int)Math.pow(2,6); y >= x*x; x+=x) {
       5        if (x % 4 > 0)
       6          y++;
       7        else
       8          y += x;
       9        System.out.println(x + "," + y);
      10      }
      11      System.out.println(x + "," + y);
      12    }
      13  }
      
    3.  1  class Foo {
       2    public static int ack(int y, int z, int x) { // notice confusing parameter order!
       3      x -= y + z;
       4      z += y - x;
       5      System.out.println(x + "," + y + "," + z);
       6      return z % (x+2*y);
       7    }
       8  
       9    public static void main(String[] args) {
      10      int x=2, y=3, z=2*y-1;
      11      for (int w=1-ack(1,2,3); w+x+y+z<11; w++) {
      12        x = ack(x, y, z);
      13        System.out.println(w + "," + x + "," + y + "," + z);
      14      }
      15    }
      16  }
  2. diceRolling
    In a file named Hw4DiceRolling.java, write a console-based Java program that solves each of the following questions using random numbers and Monte Carlo methods as we covered in class (so, in particular, you may not solve these methods by some direct mathematical expression -- you must repeatedly run a simulation with random numbers).  The user should select which problem to solve and how many attempts (rolls of the dice) there should be (which controls how accurate the answers are -- the greater the attempts, the greater the accuracy).  Your user interface must be well-designed and user-friendly.  The program should loop repeatedly, running one of these problems at the specified accuracy, until the user instead opts to quit the program.
     
    1. de Méré's Problem
      The French nobleman Chevalier de Méré, a notorious gambler, once asked the great mathematician Blaise Pascal to confirm his suspicion that the probability of getting at least one "6" in four rolls of a single 6-sided die is slightly higher than the probability of getting at least one double-six in 24 rolls of two dice.  Your program should print each of these two probabilities, their difference, and finally whether or not de Méré was correct.  Interestingly, de Méré's inquiry is believed to have led Pascal (with help from Fermat) to invent what we think of as the field of probability theory!
       
    2. Yahtzee!
      In this simplified form of the game of Yahtzee, you play as follows:
      1)  Roll 5 six-sided dice.
      2)  Leave the most-frequently-occurring dice on the table (ties go to the lower number).
      3)  Pick up the other dice (if any) and roll them again.
      4)  Once again, leave the most-frequently-occurring dice on the table (ties go to the lower number).
      5)  Pick up the other dice (if any) and roll them a third time.
      When you are done, if all the dice on the table match, you score a Yahtzee and you win!  What is the probability of winning with a Yahtzee?

      Note:  You may not store the results of the 5 rolls in 5 different variables (because it is a very bad idea!).  Instead, you must store the results of all 5 rolls in a single variable that can hold 5 different values.  Later, we will learn about arrays, which would be perfect for this task.  For now, though, you may use a randomly-generated String of length 5.  :Think about it...
       
  3. graphics:  paintingFlags
    Paint each of the following flags (one flag per Java file) using only filled rectangles and filled ovals.  You should use custom colors, matching the colors in the flags as closely as possible.  Also, your flags may not be fixed-sized, but rather they must entirely fill the window, even when the window is resized.  While the window's size may change, you may assume the window will be roughly "flag-shaped" -- you will not be graded on how your flags appear in, say, a tall thin window (which is not at all "flag-shaped").

    Note: All these flag images are from the very informational CIA World Factbook, which includes a flags-of-the-world page

    Note: As we are limited to just rectangles and ovals this week, you should replace any stars in flags with ovals (of the same size, location, and color).
     
    1. Benin
      (file: Hw4FlagOfBenin.java)
        (larger image with details)
       
    2. Norway
      (file:  Hw4FlagOfNorway.java)
      (larger image with details)
       
    3. Maldives
      (file:  Hw4FlagOfMaldives.java)
      (larger image with details)
       
    4. United States
      (file:  Hw4FlagOfUnitedStates.java)
      (larger image with details)

      Note:
        You must use loops appropriately to paint this flag, both for the stripes and for the stars!
       
  4. Bonus/OptionalpaintingDominoes
    In the file Hw4PaintingDominoes.java, paint a set of 28 dominoes matching the following figure as closely as possible (while only painting rectangles and ovals):

    Note:  The same restrictions from above apply.  In particular, you may not use drawLine, but instead must paint the white lines using fillRect in some way.

    Note:
    You must use loops appropriately here, and you also should use well-chosen helper methods (say, for painting the dots)

    Note: On close inspection, you may notice that the corners of the dominoes are rounded slightly.  You are not responsible for this effect.

Carpe diem!