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.
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:
Hint. Math.min(x, y) returns the minimum of x and y.
If all three red, green, and blue values are 0, the resulting color is black (0.0 0.0 0.0 1.0).% java RGBtoCMYK 75 0 130 // indigo cyan = 0.4230769230769229 magenta = 1.0 yellow = 0.0 black = 0.4901960784313726
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
% 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
% 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
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.