Code

boolean bMustache = true;
boolean bMonthGreaterThanTheHour = false;
int numberOfEyes = (int)random (6,10);
color potatoSkinColor = color (255, random (140,200), random (100,200));
float firstNumber = random (0,400);
float secondNumber = random (0,400);

void setup () {
  size (300,300);
  background (255);
  drawRandomLine ();
  float larger = getTheLargerOfTwoRandomNumbers ();
  println ("The larger of " + firstNumber + " and " + secondNumber + " is " + larger);
  bMonthGreaterThanTheHour = isTheMonthGreaterThanTheHour ();
  if (bMonthGreaterThanTheHour == true) {
    println ("The current Month is greater than the current hour!");
  } 
  else {
    println ("The current hour is greater than or equal to the current month!");
  }
  float average = averageOfThreeNumbers (60.0,85.0,42.0);
  println ("The average of 60, 85, and 42 equals " + average);
  drawMisterPotatoHead (numberOfEyes, potatoSkinColor, bMustache);
}


void drawRandomLine () {
  float x1 = random (0,150);
  float y1 = random (0,150);
  float x2 = random (0,150);
  float y2 = random (0,150);
  line (x1,y1, x2,y2);
}

float getTheLargerOfTwoRandomNumbers () {
  float larger = max (firstNumber, secondNumber);
  return larger;
}

boolean isTheMonthGreaterThanTheHour () {
  int currentMonth = month ();
  int currentHour = hour ();
  boolean result = currentMonth>currentHour;
  return result;
}

float averageOfThreeNumbers (float a, float b, float c){
  float average = (a+b+c)/3 ;
  return average;
}

void drawMisterPotatoHead (int eyes, color skin, boolean mustache) {
  noStroke();
  fill (skin);
  ellipse (225,225, 80,150);
  float r = red(skin);
  float g = green(skin);
  float b = blue(skin);
  color eyeColor = color (r-random(100,150),g-random(100,150),b-random(100,150));
  fill (eyeColor);
  for (int i=0; i<eyes; i++){
    ellipse (random (225-20,225+20),random (225-55,225+55),random (5,25),random (5,25));
  }
  if (mustache == true){
    fill (50,25,10);
    bezier (225-30,225+20, 225-15,225, 225+15,225, 225+30,225+20);
  } 
  fill (255);
  ellipse (225+15,225-20, 15,15);
  ellipse (225-15,225-20, 15,15);
  fill (0);
  ellipse (225+15,225-15, 5,5);
  ellipse (225-15,225-15, 5,5);

}

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