COS 126

Conditionals, Loops, Arrays
Programming Assignment

checklist

The goal of this assignment is to write five short Java programs to gain practice with loops, conditionals, and arrays.
The goal of this assignment is to write five short Java programs to gain practice with loops and conditionals.
  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 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
    

    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. Boolean and integer variables. Write a program Ordered.java that takes three integer command-line arguments, x, y, and z. Define a boolean variable whose value is true if the three values are either in strictly ascending order (x < y < z) or in strictly descending order (x > y > z), and false otherwise.
    % java Ordered 10 17 49
    true
    
    % java Ordered 49 17 10
    true
    
    % java Ordered 10 49 17
    false
    
    Do not use a conditional or loop (such as an if, while, or for statement) on this question.

  3. Type conversion and conditionals.   Several different formats are used to represent color. For example, the primary format for LCD displays, digital cameras, and web pages—known as the RGB format—specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255. The primary format for publishing books and magazines—known as the CMYK format—specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale from 0.0 to 1.0.

    Write a program RGBtoCMYK.java that converts from RGB format to CMYK format. Your program should take three integer command-line arugments red, green, and blue; print the RGB values; then print the equivalent CMYK values using these mathematical formulas:

    \( \begin{align*} white \;&=\; \max \left \{ \, \frac{red}{255}, \, \frac{green}{255}, \, \frac{blue}{255} \, \right \} \\ cyan \;&=\; \Bigl(white - \frac{red}{255} \Bigr) \; \div \; white \\ magenta \;&=\; \Bigl(white-\frac{green}{255}\Bigr) \; \div \; white \\ yellow \;&=\; \Bigl(white-\frac{blue}{255}\Bigr) \; \div \; white \\ black \;&=\; 1 - white \end{align*} \)

    Hint. Math.max(x, y) returns the maximum of x and y.

    % java RGBtoCMYK 75 0 130    // indigo
    red     = 75
    green   = 0
    blue    = 130
    cyan    = 0.423076923076923
    magenta = 1.0
    yellow  = 0.0
    black   = 0.4901960784313726
    
    % java RGBtoCMYK 255 143 0   // Princeton orange
    red     = 255
    green   = 143
    blue    = 0
    cyan    = 0.0
    magenta = 0.4392156862745098
    yellow  = 1.0
    black   = 0.0
    

    If the red, green, and blue values are each 0, the resulting color is black. Here is what your program should do:

    % java RGBtoCMYK 0 0 0       // black
    red     = 0
    green   = 0
    blue    = 0
    cyan    = 0.0
    magenta = 0.0
    yellow  = 0.0
    black   = 1.0
    
  4. Noon snooze. Write a program NoonSnooze.java that takes an integer command-line argument snooze and prints the time of day (using a 12-hour clock) that is snooze minutes after 12:00pm (noon).
    % java NoonSnooze 50
    12:50pm
    
    % java NoonSnooze 100
    1:40pm
    
    % java NoonSnooze 720
    12:00am
    
    % java NoonSnooze 11111
    5:11am
    

    Note: you may assume that snooze is a non-negative integer.

    Hint: use the integer division and remainder operators.

  5. Checkerboard. Write a program Checkerboard.java that takes an integer command-line argument n, and uses a nested for loop 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
    * * * *                           * * * * * 
     * * * *                           * * * * *
    * * * *                           * * * * * 
     * * * *                           * * * * *
                                      * * * * * 
    

  6. A drone's flight. A drone begins flying aimlessly, starting at Nassau Hall. At each time step, the drone flies one meter in a random direction, either north, east, south, or west, with probability 25%. How far will the drone be from Nassau Hall after n steps? This process is known as a two-dimensional random walk.

    1. Write a program RandomWalker.java that takes an integer command-line argument n and simulates the motion of a random walk for n steps. Print the location at each step (including the starting point), treating the starting point as the origin (0, 0). Also, print the square of the final Euclidean distance from the origin.
      Random walk in the plane
      % java RandomWalker 10         % java RandomWalker 20
      (0, 0)                        (0, 0)
      (0, -1)                       (0, 1)
      (0, 0)                        (-1, 1)
      (0, 1)                        (-1, 2)
      (0, 2)                        (0, 2)
      (-1, 2)                       (1, 2)
      (-2, 2)                       (1, 3)
      (-2, 1)                       (0, 3)
      (-1, 1)                       (-1, 3)
      (-2, 1)                       (-2, 3)
      (-3, 1)                       (-3, 3)
      squared distance = 10         (-3, 2)
                                    (-4, 2)
                                    (-4, 1)
                                    (-3, 1)
                                    (-3, 0)
                                    (-4, 0)
                                    (-4, -1)
                                    (-3, -1)
                                    (-3, -2)
                                    (-3, -3)
                                    squared distance = 18
      

    2. Write a program RandomWalkers.java that takes two integer command-line arguments n and trials. In each of trials independent experiments, simulate a random walk of n steps and compute the squared distance. Output the mean squared distance (the average of the trials squared distances).
      % java RandomWalkers 100 10000         % java RandomWalkers 400 2000
      mean squared distance = 101.446         mean squared distance = 383.12
      
      % java RandomWalkers 100 10000         % java RandomWalkers 800 5000
      mean squared distance = 99.1674         mean squared distance = 811.8264
      
      % java RandomWalkers 200 1000         % java RandomWalkers 1600 100000
      mean squared distance = 195.75          mean squared distance = 1600.13064
      

      As n increases, we expect the random walk to end up farther and farther away from the origin. But how much farther? Use RandomWalkers to formulate a hypothesis as to how the mean squared distance grows as a function of n. Use trials = 100,000 to get a sufficiently accurate estimate.

    This process is a discrete version of a natural phenomenon known as Brownian motion. It serves as a scientific model for an astonishing range of physical processes from the dispersion of ink flowing in water, to the formation of polymer chains in chemistry, to cascades of neurons firing in the brain.

  7. Dice and the Gaussian distribution. Write a program RollDice.java that takes an integer command-line argument n, and rolls 10 fair six-sided dice, n times. Use an integer array to tabulate the number of times each possible total (between 10 and 60) occurs. Then print a text histogram of the results, as illustrated below.
    % java RollDice 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: 
    

    Note: you should assume that n is a non-negative integer.

    The central limit theorem, a key result in probability and statistics, asserts that the shape of the resulting histogram tends to the ubiquitous bell curve (Gaussian distribution) if the number of dice and rolls is large.

Program style and format. Follow the guidelines in the Assignment FAQ. Is your header complete? Did you comment appropriately? Is your code indented consistently? Did you use descriptive variable names? Is your code littered with magic numbers? Did you follow the input and output specifications? Are there any redundant conditions? Did you click the Check All Submitted Files button?

Submission. Submit the files Bits.java, NoonSnooze.java, Ordered.java, RGBtoCMYK.java, Checkerboard.java, RandomWalker.java, RandomWalkers.java, RollDice.java, and a completed readme.txt file.

Challenge for the bored. Implement NoonSnooze.java without using any if statements; also, handle negative snooze times.