JavaJava: Lesson 4 – Color with the Finch


Resources available for teachers.   

New Required Java Skills: Using a random number generator

You can use a method in the Finch class to change the color of the Finch’s beak! The Finch’s beak actually has three tiny light elements inside it. One is red, one is green, and one is blue. This is important for programming the beak. To change the color of the beak, use the setLED() method.

The setLED() method requires four parameters. The red parameter controls the amount of  red light from 0 (none) to 255 (maximum brightness). The green  and blue parameters control the amount of green and blue light, respectively, from 0 to 255. By combining different amounts of these three colors, you can also create other colors. For example, red light and blue light will combine to make purple. The fourth parameter sets the time in milliseconds that the beak should stay this color. The beak will turn off when this time is up.

Exercise 1:

Try the command shown above. The Finch’s beak should turn red for two seconds.

Exercise 2:

Write a program to make the beak turn purple, then aqua, then yellow.

Exercise 3:

Write a program that asks the user for the amount of red, green, and blue in the beak color. The beak should stay that color for three seconds.

You can also use the setLED() method with only the first three parameters (red, green, and blue). When used with three parameters, this method will set the beak color and then move on immediately to the next line in the program.

Exercise 4:

What is the difference between the two code snippets shown below? Make a prediction and then test it. Be sure that you can explain why these pieces of code cause the Finch to behave differently.

Exercise 5:

Write a program to make the Finch move forward, then back. When the Finch is moving forward, its beak should be green. When the Finch is moving back, its beak should be red.

Exercise 6:

Make the Finch into a spinner for a game like Twister or Chutes and Ladders. Create a program that spins the Finch for a random amount of time; you will need to use a random number generator to implement this program. The Finch’s beak should be red while it is spinning and green when it stops.