CS 40 - Lecture 3

Cay S. Horstmann
Lecture 2 Recap

- Deal with complexity: Dive in, solve a task, don't try to
understand everything
- Programming: Create a sequence of steps that can be replayed
- Objects have observable behavior. Ex. toaster
- Methods are invoked on objects. Ex. toaster.move
- Object-oriented design: Put the methods with the responsible
objects
Survey Questions (3 slides)
Please log in to ActiveLecture.org. Click
Discuss, type A B C, click Send.
What computer can you use outside class?
- I have a laptop
- I have a desktop at home/in my dorm
- I need to use a computer in a computer lab
How Can You Get Alice Outside Class?
- I already installed it
- I brought my laptop today so I can get installation help
- I will bring my laptop next Tuesday so I can get help
- I will install it myself over the weekend and use the eCampus
discussion feature if I have problems
- I need to use a computer in a lab that has Alice preinstalled
How Was the Lab Work on Tuesday?
- Too easy/boring
- Just right
- Too hard/confusing
Thanks for your feedback!
From Scenario to Code
- Scenario: After traveling through space, a
robot-manned craft has just made a landing on a moon. The robot is on the
moon and has set up a camera so earthbound scientists in Houston can view
this historic event. The camera view shows the robot, the lunar lander,
and some nearby rock formations. Suddenly an alien peeks out from behind
a rock, surprising the robot. The robot looks around, spots the alien,
and walks over to take a closer look. The alien is frightened and hides
behind the rocks. The robot sends a message back to Houston.
- Problem: How do we code this in Alice?
Create Initial World

- Make a new world
- Choose a template for the background
- Add objects
- Position objects to their initial location
- Adjust camera if necessary
Break Scenario Into Scenes

- Like in a movie script
- Example scene: Alien peeks from behind rock
- Another example: The robot sends a message back to Houston.
- For each scene,
- make a rough sketch
- write a title
- list the actors
- list their actions
Break Scene Into Tasks

- Goal: identify simple tasks that the Alice objects can do
- Move (up, down, sideways), turn
- Speak
- Change color
- Do something with body parts
- Also consider turning the camera at the beginning of the scene
Top-Down Design

- Start with written scenario
- Break down into scenes
- Break each scene into tasks
- Translate into program
Code Reading

- Download from FirstEncounter.a2w
- Many students...and professional programmers...have a hard time reading
code
- Takes practice
- Learn and lift. “Lesser artists borrow. Great artists
steal.”—Igor Stravinsky
Code Reading

Code Reading
What is the purpose of those long expressions
spiderRobot.body.xxxLegLegBase.xxxXxxLegUpperjoint.turn...
in plain English? (Answer in ActiveLecture.org)
Code Reading
Some statements are in a do together block, others in a do in
order block.
What would happen if they were all in a do in
order block? (Answer in ActiveLecture.org)
Lab Work
Add to the "First Encounter" Story.
Scene 1. The alien reappears. The robot says "Take me to your leader".
Scene 2. The alien and the robot walk away into the distance.
Hint: You may need to turn the camera when they walk away.
Reminder

- Save your world (FirstEncounter.a2w)
- Upload to eCampus
- When working with a buddy, put in the buddy names (first and last!) in
the comment field.
- Tonight, put your photo and bio into eCampus