Code

void setup(){
  smooth();
  size (300, 300);
  drawRandomLine ();
  float num = getTheLargerofTwoRandomNumbers ();
  println ("number = " + num);
  boolean isit = isTheMonthGreaterThanTheHour ();
  println ("Is it true or false? " + isit);
  float avgof3 = averageOfThreeNumbers (1, 2, 3);
  println (avgof3);
  drawMisterPotatoHead (5, color(30,200,200), true);
}

void drawRandomLine(){
  line (random(20), random(20), random (20), random(20));
}

float getTheLargerofTwoRandomNumbers (){
  float a = random(201923912);
  float b = random(209192130);
  float big;
  if (a > b){
    big = a;
  }
  else if (b > a){
    big = b;
  }
  else { //if a and b are equal, big is set to 0
    big = 0;
  }
  return big;
}

boolean isTheMonthGreaterThanTheHour(){
  int h = hour();
  int m = month();
  boolean which;
  if (m > h) {
    which = true;
  }
  else {
    which = false;
  }
  return which;
}

float averageOfThreeNumbers (int j, int k, int l){
  float avg = (j + k + l)/3;
  return avg; 
}

void drawMisterPotatoHead (int number, color col, boolean TorF){
  fill (col);
  ellipse (width/2, height/2, 200, 275);
  for (int i = 0; i < number; i++){
    fill (255);
    float ex = (width/2) + random (-50, 50);
    float ey = 100 + random (-30, 30);
    ellipse (ex, ey, 25, 50);
    fill (0);
    ellipse (ex, ey, 10, 20);
  }
  if (TorF == true){
    triangle (100, 220, width/2, 190, 200, 220);
  }
  fill (255, 0, 0);
  ellipse (width/2, height/2, 50, 50);
}

0401 - Function review: All manner of functions

Statement:This assignment asks you to create and use several functions. In addition to defining the code for the functions themselves, please also prove that they work correctly by calling/invoking them from your setup function. If you test a function by printing a result, make sure that your print command is not in the code of the function itself, but in the code which calls/invokes your custom function.

  • Create a function drawRandomLine, which takes no arguments, and which returns no values, but which draws a small random line in the upper left corner of the canvas.
  • Create a function called getTheLargerOfTwoRandomNumbers, which takes no arguments, but which generates two random numbers, and then returns the larger of these.
  • Create a function called isTheMonthGreaterThanTheHour, which takes no arguments, but which returns a true-or-false value indicating whether or not the month (numbered 1-12) is larger than the hour (numbered 0-23). To do this properly, go look up the month() and hour() functions in the Processing reference.
  • Create a function called averageOfThreeNumbers, which takes three integer arguments, and which returns a number which is the exact mathematical average of the inputs. Keep in mind that the answer could have a fractional component.
  • Create a function called drawMisterPotatoHead which draws a face of sorts, and which returns no value, but which takes the following three arguments: a round (whole) number; a color; and a true-or-false value. Use the round number to govern the number of eyes which are rendered (minimally, it should draw something reasonable-looking with up to 5 eyes). Use the color to change the color of the potato skin. Use the true-false value to govern whether a mustache is present.

    Broadly speaking, your code will look approximately like the following:

    void setup(){
      doSomething();
      println( doThingThatReturnsTrue() );
    }
    
    void doSomething(){
      // what goes here?
    }
    
    boolean doThingThatReturnsTrue(){
      return true;
    }
    

    hide statement