COS 126

Conditionals and Loops
Programming Assignment


The goal of this assignment is to write four short Java programs to gain practice writing expressions, loops, and conditionals.

  1. Type conversion. There are a number of different formats for representing color. RGB format specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255: It is the primary format for LCD displays, digital cameras, and web pages. CMYK format specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale of 0.0 to 1.0: It is the primary format for publishing books and magazines. Write a program RGBtoCMYK.java that reads in three integers red, green, and blue from the command line and prints out equivalent CMYK parameters.

    The conversion is a two stage process. First, use the red, green and blue values to compute the intermediate values c, m, y and k. Then use those intermediate values to compute the final cyan, magenta, yellow and black values. The mathematical formulas for converting from RGB to an equivalent CMYK color are:

    RGB to CMYK formula

    Hint. Math.min(x, y) returns the minimum of x and y.

    % java RGBtoCMYK 75 0 130       // indigo
    cyan    = 0.4230769230769229
    magenta = 1.0
    yellow  = 0.0
    black   = 0.4901960784313726
    
    If all three red, green, and blue values are 0, the resulting color is black (0.0 0.0 0.0 1.0).

  2. Olympic Gymnastic Averages. Write a program OlympicAverage.java that takes 7 doubles as command line arguments. The first is the difficulty and technical content score. The remaining six are the scores from each of the judges (for execution, artistry, composition and technique). Your program should print out the final score which is the difficulty score plus the average of the judges' scores excluding the highest and lowest.
    For example, Liukin's score in the uneven bars for women:
    7.7 9.0 9.3 9.0 8.8 9.0 9.1
    is 7.7 + (9.0 + 9.0 + 9.0 + 9.1) / 4
    and the result is 16.725
    % java OlympicAverage 7.7 9.0 9.3 9.0 8.8 9.0 9.1
    The final score is 16.725
    

  3. A drunkard's walk. A drunkard begins walking aimlessly, starting at a lamp post. At each time step, the drunkard forgets where he or she is, and takes one step in a random direction, either north, east, south, or west, each with probability 25%. How far will the drunkard be from the lamp post after N steps? Random walk in the plane

  4. Write a program RandomWalkers.java that takes two command-line arguments N and T. In each of T independent experiments, simulate a random walk of N steps and compute the squared distance. Output the mean squared distance (the average of the T squared distances).
    % java RandomWalkers 100 100000         % java RandomWalkers 400 100000
    mean squared distance = 100.15086       mean squared distance = 401.22024
    
    % java RandomWalkers 100 100000         % java RandomWalkers 800 100000
    mean squared distance = 99.95274        mean squared distance = 797.5106
    
    % java RandomWalkers 200 100000         % java RandomWalkers 1600 100000
    mean squared distance = 199.57664       mean squared distance = 1600.13064
    

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

    Remark: 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.