CS 40 - Lecture 5

Cay S. Horstmann
Lecture 4 Recap

- condition: an expression that is either true or false
- if-else decision: Either execute the statements in the
if part or in the else part.
- while loop: Keep repeating the statements while a
condition is true
Alice Install Survey
Please log in to ActiveLecture.org. Click
Discuss, type A B C, click Send.
- I already installed Alice
- I brought my laptop today so I can get installation help
- Help! I don't know how to get Alice to work at home
How Was the Lab Work on Tuesday?
- Too easy/boring
- Just right
- Too hard/confusing
Thanks for your feedback!
Sample solutions are in http://www.horstmann.com/sjsu/cs40/schedule.html
Methods

- A method contains a sequence of instructions (e.g. move up, move right,
move down)
- A method has a name (e.g. hop)
- A method belongs to an object (e.g. the toaster)
- To execute the statements, the method must be called (e.g.
toaster.hop())
Example: SpiderRobot

- Load the First Encounter world
again
- Note how long the my first world method is
- Note the Do together tile with the comment
// spiderRobot moves forward as its legs walk
- Comment = English (not Alice code!) to help the human code reader
- These statements can be put into a spiderRobot method
Lab # 1: Walk Method
- Drag the Do together tile to the clipboard (top right corner
of screen)
- Click on spiderRobot
- Click on create new method
- Name it walk
- Drag the clipboard into the walk method
- Go back to world.my first method and drag the Do
together tile into the trash can
- Click on spiderRobot. Drag the walk tile into my
first method.
Try It Out
- Hit Play. What happens?
- Why is this version of the program better than the original?
- How many meters does the robot walk?
- What happens if you place the walk tile twice?
Put answers to 1., 2., 3., 4. into http://ActiveLecture.org
Too Boring?
- Make the robot walk all the way to the rock, using the walk
method and a while loop.
Parameters
- The walk method makes the robot walk 1 meter
- What if we want to walk 10 meters? 3.5 meters?
- Why not walk arbitrary distances?
- Make distance a parameter
- Parameter = input to method
- Other example of parameter: say method has parameter
what (i.e. what to say)
spiderRobot.say("Houston, we have a problem!")
Lab #2: Distance Parameter
- In spiderRobot.walk, click on create new parameter
- Call the parameter distance
- Make it a number
- Drag the parameter over the 1 meter value in the call to move
- Click Play. The robot moves 1 meter. (Look into my first
method to see why.)
- In my first method, set parameter value to 5
- Click Play. The robot moves 5 meter...
Not So Fast!
- ...but it moves all 5 meter in a single second.
- You want to traverse 5 meters in 5 seconds
- But your method needs to work for any distance
- Fix this! (Hint: Click on more, select expression)
Too Boring?
.jpg)
- Make another method announce for the spiderRobot
- Your method should do the last three tiles in my first method:
face the camera, turn the head red, say something
- The “something” should be a parameter (of type
String)
- Make the robot announce “Houston, we have a problem!”
followed by “The end.”, by calling the announce
method twice.
- Still bored? Make the head color into a second parameter
Food for Thought

- In lecture 4, the evil step sister either moved to cookie and ate it,
or it moved to cookie2 and ate that.
- What method could one make to simplify this code?
- What would the parameter be?
- What is the parameter type? Number, true/false, String, Color,
Object?
- Try it out in your copious spare time...
Reminders

- Homework #1 is in eCampus