Kotlin & Processing basics

Start: 10/3/2023
Due: 10/30/2023, by the beginning of class
Collaboration: individual
Important Links: [how to make your mac ready for MCS-178] [how to make your Linux machine ready for MCS-178] [style guidelines] [readme.txt] [gradesheet] [zip how-to] [zipped gradle project]


Overview

The goal of this assignment is to practice writing short Kotlin programs that

- use loops
- use conditional statements
- use the processing library functions
- process command line arguments

Before starting, download the zipped gradle project and expand it into your mcs178 directory. Study its content and make sure you know where to put the source files you’re supposed to write. Also make sure you know how to issue the gradle commands to compile and run your programs. We’ll demonstrate these commands in class.

The programs

  1. Checkerboard. Make sure your terminal’s current working directory is in the project root directory. Use your text editor to write a program ./Checkerboard/src/main/kotlin/Checkerboard.kt that takes an integer command line argument n, and uses a nested loop construct to output an n-by-n checkerboard pattern like the ones below:

    $ gradle -q :Checkboard:run --args=4
    4x4 checkerboard

    $ gradle -q :Checkboard:run --args=5
    5x5 checkerboard

    The convention is that the lower left corner square is always black. Note that your program must work correctly for any positive integer n, not just for n equal to 4 or 5. Additionally, your program should check that the user provides exactly one command line argument (as a positive integer) before continuing. If this condition is not met, the program should give an error message and abort.

  2. Bears. This exercise has two parts.

    1. Write a Kotlin program ./Bear/src/main/kotlin/Bear.kt that draws a cute cartoon face of a bear like the following.

      $ gradle -q :Bear:run
      Bear face

    2. By changing the arguments of all shape-drawing functions calls, turn your bear face drawing code into the function

      fun bear1(x: Float, y: Float, w: Float, h: Float)

      so that when bear1(x, y, w, h) is called, a bear face of width w, height h will be drawn with its upper left corner at (x, y). For example, this example run
      > $ gradle -q :Bears:run
      Bear faces
      results from the fact that the file Bears.kt contains the line size(800, 400) inside its settings() definition, and the lines

          bear1(60F,  80F, 200F, 200F)  
          bear1(260F, 20F, 200F, 150F)  
          bear1(280F, 180F, 150F, 200F)  
          bear1(450F,  40F, 300F, 300F)  

      inside its draw() definition.

    3. Now implement a function

      fun bear2(x: Float, y: Float, w: Float, h: Float)

      by modifying the Bear.kt code so that calling bear2(x, y, w, h) works exactly like bear1(x, y, w, h). However, bear2 should use the translate and scale functions, and retains most of the bear face drawing code from Bear.kt.

  3. Random walks. 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. It 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.

    1. Write a Kotlin program ./RandomWalker/src/kotlin/RandomWalker.kt that obtains an integer n from its command line argument and simulates the motion of a random walk for n steps. Draw the location point at each step (including the starting point), treating the starting point as the Cartesian Coordinates system origin (0, 0). Also, print the square of the final Euclidean distance from the origin. Here is an example run with n = 5000.

      $ gradle :RandomWalker:run --args=5000
      random walk
      squared distance = 1250.0

      Make sure the program checks the command line argument(s) and gives appropriate error message if not exactly one argument is given.

    2. Write a program ./RandomWalkers/src/kotlin/RandomWalkers.kt that obtains two integers n and trials from its command line arguments. In each of trials independent experiments, simulate a random walk of n steps and compute the squared distance (without using the processing library to display the points.). Output the mean squared distance (the average of the trials squared distances). Here are example runs of the program on various values of n and trials.

      $ gradle -q :RandomWalkers:run --args="100 10000"
      mean squared distance = 101.446
      
      $ gradle -q :RandomWalkers:run --args="400 2000"
      mean squared distance = 383.12
      
      $ gradle -q :RandomWalkers:run --args="100 10000"
      mean squared distance = 99.1674
      
      $ gradle -q :RandomWalkers:run --args="800 5000"
      mean squared distance = 811.8264
      
      $ gradle -q :RandomWalkers:run --args="200 1000"
      mean squared distance = 195.75
      
      $ gradle -q :RandomWalkers:run --args="1600 100000"
      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 the output from 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.

      Make sure the program checks the command line argument(s) and gives appropriate error message if not exactly two arguments are given.

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?

Tips

Gradesheet

We will use this gradesheet when grading your project.

Submission

You should create a “zip” archive file containing all 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. If you are going to zip up the whole basics directory, make sure you issue the command

    $ gradle clean

in the project root directory before issuing the zip command in the directory containing the basics project. Ask your lab instructor for help if you need it. Submit the zip file via Moodle.