Lecture 4

  • More conditionals

  • To make our basketball game a bit more exciting, let's add something that is clearly missing from our first version -- a ball. In particular, we would like to change the game so that to score you have to drag the ball into the basket. Click on BetterBasketBall to show the example.
  • note how dragging works
  • note use of nested if-statement
  • Can improve further by using a boolean variable to remember whether carrying ball. Click on EvenBetterBasketBall to show the example.
  • Note how boolean operator && (and) is used. Can also use || (or) and ! (not).
  • Random numbers and the if-else

  • To generate a random integer between 1 and 4 use the RandomIntGenerator class as follows:
           RandomIntGenerator intGen = new RandomIntGenerator(1,4)
        
  • A new version of the scribbler program that draws in four different colors. Click on Color4Scribbler to show the example.
  • Note how the color is stored in the variable currentColor
  • A fancier version of the scribbler program that generates a random color by selecting the red, green, and blue components at random by using a random number generator that selects a number between 0 and 255. Click on ScribbleColor to show the example.
  • Note how new colors are created.