COS 126

Conditionals, Loops, Arrays
Programming Assignment


The goal of this assignment is to write five short Java programs to gain practice with loops, conditionals, and arrays.

  1. Bits. Write a program Bits.java that takes an integer command-line argument N and uses a while loop to compute the number of times you need to divide N by 2 until it is strictly less than 1. Print out the error message "Illegal input" if N is negative.
    % java Bits 0                     % java Bits 8
    0                                 4
    
    % java Bits 1                     % java Bits 16
    1                                 5
    
    % java Bits 2                     % java Bits 1000
    2                                 10
    
    % java Bits 4                     % java Bits -23
    3                                 Illegal input
    

    Remark: This computes the number of bits in the binary representation of N, which also equals 1 + floor(log2 N) when N is positive. This quantity arises in information theory and the analysis of algorithms.


  2. Checkerboard. Write a program Checkerboard.java that takes an integer command-line argument N, and uses two nested for loops to print an N-by-N "checkerboard" pattern like the one below: a total of N2 asterisks, where each row has 2N characters (alternating between asterisks and spaces).
    % java Checkerboard 4             % java Checkerboard 5
    * * * *                           * * * * * 
     * * * *                           * * * * *
    * * * *                           * * * * * 
     * * * *                           * * * * *
                                      * * * * * 
    


  3. Probability of having at least one child of each gender. A couple beginning a family decides to keep having children until they have at least one of either sex.

    1. BoyAndGirl. Write a program BoyAndGirl.java that simulates repeatedly having a child until there is at least one of each sex. Output how many children it took to achieve this goal. Assume there is equal probability of having either a boy or a girl. Use a while loop to compute the number of children the couple has until getting at least one of each gender.
      % java BoyAndGirl
      Congratulations!  You have 3 children.
      
      % java BoyAndGirl
      Congratulations!  You have 6 children.
      
      % java BoyAndGirl
      Congratulations!  You have 2 children.
      

    2. BoysAndGirls. Write a program BoysAndGirls.java that takes an integer command-line argument T. In each of T independent experiments, simulate a couple having children until they have at least one of each gender. Use the results of the T experiments to estimate the average number of children the couple will have. Record and output the frequency counts for 2, 3, and 4 children, and also one count for 5 and above. Finally, output the most common number of children in a family. (If there is a tie, print only the first most common number of children.) As before, assume that the probability of having a boy or a girl is 1/2.
      java BoysAndGirls 2
      Average: 6.0 children to get at least one of each sex.
      Number of families with 2 children: 0
      Number of families with 3 children: 0
      Number of families with 4 children: 0
      Number of families with 5 or more children: 2
      Most common number of children is 5 or more.
      
      % java BoysAndGirls 10
      Average: 3.5 children to get at least one of each sex.
      Number of families with 2 children: 2
      Number of families with 3 children: 3
      Number of families with 4 children: 3
      Number of families with 5 or more children: 2
      Most common number of children is 3.
      
      % java BoysAndGirls 100
      Average: 3.19 children to get at least one of each sex.
      Number of families with 2 children: 44
      Number of families with 3 children: 24
      Number of families with 4 children: 16
      Number of families with 5 or more children: 16
      Most common number of children is 2.
      
      

    As T increases, we expect the average number of children per family to converge. Use BoysAndGirls to formulate a hypothesis as to what this average is. Run BoysAndGirls with T = 1, 10, 100, 100000 and 1000000 to watch it converge to a sufficiently accurate estimate.


  4. Dice and the Gaussian distribution. Write a program TenDice.java that takes an integer command-line argument N, and rolls 10 fair six-sided dice, N times. Use an array to tabulate the number of times each possible total (between 10 and 60) occurs. Then print out a text histogram of the results, as illustrated below.
    % java TenDice 1000
    10: 
    11: 
    12: 
    13: 
    14: 
    15: 
    16: 
    17: 
    18: *
    19: ****
    20: 
    21: ***
    22: ******
    23: ********
    24: ****************
    25: *************
    26: **********
    27: *********************************
    28: ****************************************
    29: *********************************
    30: ***************************************************
    31: *****************************************************************
    32: ********************************************************
    33: **************************************************************************************
    34: ***********************************************************
    35: *********************************************************************
    36: ***********************************************************************************
    37: **************************************************************
    38: *****************************************************************
    39: ***************************************
    40: *****************************************************
    41: ************************************
    42: ****************************
    43: ************************
    44: ************************
    45: *********
    46: ***********
    47: *******
    48: ***
    49: **
    50: 
    51: 
    52: *
    53: 
    54: 
    55: 
    56: 
    57: 
    58: 
    59: 
    60: 
    
    Remark: a classic result from probability theory (central limit theorem) asserts that the shape of the resulting histogram is well-approximated by the ubiquitous bell curve (Gaussian distribution).


Submission. Submit the files Bits.java, Checkerboard.java, BoyAndGirl.java, BoysAndGirls.java, and TenDice.java. Finally, submit a readme.txt file and answer the questions.