1. Conditionals & Loops
Download Project
 Submit to TigerFile
Goals
 To deepen your understanding of loops, conditionals, and arrays.
 To learn how to debug code.
Background
This assignment is based on the concepts and techniques described in lecture and precept; Section 1.3 of the textbook provides additional details and examples. You may also find it instructive to work through some of the other exercises and look at the solutions on the booksite afterwards.
Requirements

Download and unzip the project
zip
file, which contains the files you will need for this assignment. Download the projectzip
file for this assignment from TigerFile by right clicking on theloops.zip
link (see below), and selecting save link as… 
Expand the zip file, which will create the folder
loops
. We recommend this folder be placed in yourCOS126Assignments
folder. 
Implement five programs:
Ordered.java
RGBtoCMYK.java
Checkerboard.java
RandomWalker.java
RandomWalkers.java

Complete a
readme.txt
. 
Submit your
.java
,readme.txt
andacknowledgments.txt
files using TigerFile . 
Read the COS 126 Style Guide to ensure your code follows our conventions. Style is an important component of writing code, and not following guidelines will result in deductions.
Ordered
This exercise demonstrates the use of the int
and boolean
data types. Write a program Ordered.java
that takes three integer commandline 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. Then, print this boolean
value. Do not use a conditional or loop (such as an if
, while
, or for
statement) in your solution.
Examples:
> javaintrocs Ordered 10 17 49
true
> javaintrocs Ordered 49 17 10
true
> javaintrocs Ordered 10 49 17
false
RGBtoCMYK
This exercise demonstrates the use of type conversions. 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 inclusive. 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 inclusive.
Write a program RGBtoCMYK.java
that converts from RGB format to CMYK format. Your program must take three integer commandline arguments, red, green, and blue; print the RGB values; then print the equivalent CMYK values using the following mathematical formulas.
$$ white = \max ({ \frac{red}{255}, \frac{green}{255}, \frac{blue}{255}}) $$
$$ cyan = (white  \frac{red}{255}) \div white $$
$$ magenta = (white  \frac{green}{255}) \div white $$
$$ yellow = (white  \frac{blue}{255}) \div white $$
$$ black = 1  white $$
CMYK is a subtractive color space, because its primary colors are subtracted from white light to produce the resulting color: cyan absorbs red, magenta absorbs green, and yellow absorbs blue.
Examples:
// indigo // Princeton orange
> javaintrocs RGBtoCMYK 75 0 130 > javaintrocs RGBtoCMYK 255 143 0
red = 75 red = 255
green = 0 green = 143
blue = 130 blue = 0
cyan = 0.423076923076923 cyan = 0.0
magenta = 1.0 magenta = 0.4392156862745098
yellow = 0.0 yellow = 1.0
black = 0.4901960784313726 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:
// black
> javaintrocs RGBtoCMYK 0 0 0
red = 0
green = 0
blue = 0
cyan = 0.0
magenta = 0.0
yellow = 0.0
black = 1.0
Checkerboard
Write a program Checkerboard.java
that takes an integer commandline argument \(n\), and uses a nested for loop to print an nbyn checkerboard pattern like the one below: a total of \(n^2\) asterisks, where each row has \(2n\) characters (alternating between asterisks and spaces).
> javaintrocs Checkerboard 4 > javaintrocs Checkerboard 5
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * *
RandomWalker
A drone begins flying aimlessly, starting at Nassau Hall (0, 0). 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 twodimensional random walk.
Write a program RandomWalker.java
that takes an integer commandline 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). Finally, print the square of the final Euclidean distance from the origin.
Examples:
> javaintrocs RandomWalker 10 > javaintrocs 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
Click to show possible progress steps
 Think about which variables you need to maintain. You certainly need to parse and store the commandline argument
n
. You also need to store the current location (x, y) of the random walker. What should be the type of the variablesx
andy
? What should be their initial values?  To choose a random direction, consider using the idiom from Section 1.2 to generate a random integer in a given range.
RandomWalkers
Write a program RandomWalkers.java
that takes two integer commandline 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).
Background
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.
Examples:
> javaintrocs RandomWalkers 100 10000
mean squared distance = 101.446
> javaintrocs RandomWalkers 100 10000
mean squared distance = 99.1674
> javaintrocs RandomWalkers 200 1000
mean squared distance = 195.75
> javaintrocs RandomWalkers 400 2000
mean squared distance = 383.12
> javaintrocs RandomWalkers 800 5000
mean squared distance = 811.8264
> javaintrocs RandomWalkers 1600 100000
mean squared distance = 1600.13064
Click to show possible progress steps

Copy the code (in
main
) fromRandomWalker.java
toRandomWalkers.java

Which additional variables do you need?
 You certainly need to read and store the commandline arguments
n
andtrials
.  In addition to the current location (x, y) of the random walker, you need an accumulator variable, say
totalSquaredDistance
, that stores the total sum of squared distances so far.  Nest the loop inside an outer loop that repeats trials times and add code to update
totalSquaredDistance
after each time through the outer loop.
readme.txt
 Analysis
Edit the text file named readme.txt
that is a narrative description of your work.
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\).
What is the proportional relationship between the number of steps \(n\) of the random walk and the mean squared distance? By relationship, we mean something like
mean squared distance = \(126n^9\)
Briefly justify your answer based on computational experiments. Describe the experiments and list several data points. Provide your answer in your readme.txt
file.
Hints!
 Run a series of experiments keeping the number of trials fixed, but changing \(n\)  try large values for \(n\) (e.g., 100,000)
 Run a series of experiments keeping \(n\) fixed but changing the number of \(trials\)  try large values for \(trials\) (e.g., 100,000)
Submission
Submit the files Ordered.java
, RGBtoCMYK.java
, Checkerboard.java
, RandomWalker.java
and RandomWalkers.java
, along with completed readme.txt
and acknowledgments.txt
files to TigerFile
.
Enrichment
Here are some famous and notsofamous quotations about learning to program.