Conditionals and Loops
Start: 9/20/2018
Due: 10/1/2018, by the beginning of class
Collaboration: individual
Important Links: style guidelines, readme.txt, gradesheet
From Robert Sedgewick & Kevin Wayne, Princeton University. Original language was java.
Overview
The goal of this assignment is to write five short kotlin programs to gain practice with loops, conditionals, and arrays.
The Programs
Boolean and Int variables. Write a program
Ordered.kt
that takes threeInt
command line arguments, x, y, and z. Define aBoolean
variable whose value istrue
if the three values are either in strictly ascending order (x < y < z) or in strictly descending order (x > y > z), andfalse
otherwise, then print out that variable.$ kotlin OrderedKt 10 17 49 true $ kotlin OrderedKt 49 17 10 true $ kotlin OrderedKt 10 49 17 false
Do not use a conditional or loop (such as an
if
orwhile
statement) on this question. Testing whether or not the condition (x < y < z) or (x > y > z) holds can be accomplished using 4 operations. You will get extra credit points if you can find such a solution.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.kt
that converts from RGB format to CMYK format. Your program should take three integer command line arugmentsred
,green
, andblue
; 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.
kotlin.math.max(x, y)
returns the maximum ofx
andy
. If you write the lineimport kotlin.math.max
at the beginning of your
RgbToCmyk.kt
file, you would be able to writemax
instead ofkotlin.math.max
whenever you want to call the function.$ kotlin RgbToCmykKt 75 0 130 $ kotlin RgbToCmykKt 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:
$ kotlin RgbToCmykKt 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.kt
that takes an integer command line argument n, and uses a nestedfor
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).$ kotlin CheckerboardKt 4 $ kotlin CheckerboardKt 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
A drone's flight. A drone begins flying aimlessly, starting at Pittman 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 Pittman Hall after n steps? This process is known as a two-dimensional random walk.
Write a kotlin program
RandomWalker.kt
that obtains an integern
from its command line argument and simulates the motion of a random walk forn
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.$ kotlin RandomWalkerKt 10 $ kotlin RandomWalkerKt 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
Write a program
RandomWalkers.kt
that obtains two integersn
andtrials
from its command line arguments. In each oftrials
independent experiments, simulate a random walk ofn
steps and compute the squared distance. Output the mean squared distance (the average of thetrials
squared distances).$ kotlin RandomWalkersKt 100 10000 $ kotlin RandomWalkersKt 400 2000 mean squared distance = 101.446 mean squared distance = 383.12 $ kotlin RandomWalkersKt 100 10000 $ kotlin RandomWalkersKt 800 5000 mean squared distance = 99.1674 mean squared distance = 811.8264 $ kotlin RandomWalkersKt 200 1000 $ kotlin RandomWalkersKt 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? UseRandomWalkersKt
to formulate a hypothesis as to how the mean squared distance grows as a function ofn
. Usetrials
= 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.
Program style and format. Follow the guidelines in the Style Guidelines. Is your header complete? Did you comment appropriately? Is your code indented consistently? Did you use descriptive variable names? Did you follow the input and output specifications? Are there any redundant conditions?
Gradesheet
We will use this gradesheet when grading your lab.
Submission.
You should create a “zip” archive file containing all five kotlin programs and your completed readme.txt
file. Be sure to include the completed readme.txt file in your zip file. Read zip how-to if you do not know how to create a zip file. Ask your lab instructor for help if you need it.
Submit the zip file via Moodle.