Intro To Java Workshop
Section 4 (Alternate)
While loop activity
- Print a few copies of this worksheet (editable version here) before class.
- Do an example with students: roll one die and write the result in the first box. Roll two dice and write the sum in the second box. Walk through the code and figure out what will be printed.
- Divide students into small groups and give each group a worksheet. Ask them to work together to roll the dice, fill out the worksheet, and figure out what the code will print.
- Have students explain their answers. Once they have it correct, dispense the corresponding number of animal crackers.
Stephen Hawking
Remind students of the three types of variables they have learned about. Review how to declare and initialize a string.
Explain the speak()
method, and relate it to calling move()
or turn()
but with a String
parameter
instead of an int
parameter.
Have students complete Stephen Hawking in zebra.section4 package.
Zombie Eyes
Race
Make sure sound library is installed: "Tools" -> "Add tool..." -> "Libraries" Tab -> Filter for "sound" If the "Sound" library has a green checkbox next to it, it is installed. If it does not, click the library and click "Install"
- Tell students they will be making a game where two people can race a dog and a cat across the screen by each tapping a keyboard button as fast as they can
- Ask students to call
moveDog()
in thesetup()
method. The dog will move just a tiny bit because thesetup()
method is called just once. - Ask students to move the call to
moveDog()
in thedraw()
method. The dog will keep moving because thedraw
method is called repeatedly. - But, we don't want the dog to move constantly--we want it to move
only when we hit a button. Explain that the
key
variable keeps track of the last button that was hit, so we can writeif (key == 'a'){...}
Final code should have something like this in the draw()
method:
if (key == 'a'){
moveDog();
}
if (key == 'l') {
moveCat();
}
Optional extra activities for students who finish early:
- Change the background image
- Make the animals move faster
- Change the text that appears when someone wins