CS 40 - Lecture 7

Cay S. Horstmann
Lecture 6 Recap

- voting machine: a machine that tallies votes
- computer science: impossible to verify that a computer program
works correctly; failures must be expected
- public policy implication: must build in verification (random
spot checks) and redundancy (independent paper trail)
Variables

- A variable holds a value
- The value can change over time
- Initializing the variable = setting the first value
- Instance variable is defined in object
- Local variable is defined in method
Example: Voting Machine

- Load the VotingMachine1 world
again
- The voting machine needs to keep track of the votes
- Where are the variables?
- Answer in ActiveLecture.org
What Kind of Variables?
What are the variables in the voting machine?
- Local variables
- Instance variables
Answer in ActiveLecture.org
Lab # 1: Combo Lock

- Load the ComboLock1 world.
- Click Play, then hit the left and right arrow keys ← and
→. What happens?
- Why does it happen? Hint: Close the Play window. Look at the events.
Look at the methods.
- Answer in ActiveLecture.org
Track the Number
- We want to track the number under the red arrow.
- The combo lock doesn't know it. The dial is just a 3D image.
- Need a variable.
- Need an instance variable because we store object state
(accessible by multiple methods)
Make an Instance Variable
- Click on the comboLock tile, then the properties tab in
comboLock details. Click on create new variable.
- Name the variable dialValue. It is supposed to denote the
current value of the dial.
- Click on the methods tab, then edit the up method.
- Click again on the properties tab and drag the dialValue
tab into the up method.
- cont'd on next slide...
Make an Instance Variable (Cont'd)
- Pick comboLock.dialValue++ from the menu. (++ is shorthand for
“increment” or “add 1”)
- Be sure to keep the properties tab visible and click Play. Hit
the right arrow key →.
- What happens with the value of dialValue in the properties
tab? (ActiveLecture.org)
Initializing
- If you carefully look at the dial and the value of the variable, what
do you notice?
- How do you fix it? (ActiveLecture.org)
Changing Values
- Also implement the down method. Simply pick
comboLock.dialValue--
- What happens when you keep hitting the left arrow key?
- That isn't so good. What do you do to fix it?
- Do you also need to put a fix in the up method?
Answers in ActiveLecture.org
Too Boring?
.jpg)
- To select a number in a real combo lock, you reverse direction when you
reach the number.
- We want to remember the last selected number.
- We also need to track the direction so we can tell when it is
reversing.
- Make two variables. direction is +1 when moving up, -1 when
moving down. When do you set selectedNumber?
- How do you initialize direction and selectedNumber?
Why Local Variables?
Lab #2: Local Variables
- Start with the sample solution lab05.a2w
- Make a local variable by clicking on create new
variable
- Initialize backJoint with the horrid expression
- Replace the horrid expression with backJoint twice
- Do the same with frontJoint
- Click Play to make sure it works
- If you have extra time, move all four joints.
Reminders

- Lab work in eCampus
- Is your bio/photo in eCampus?.
- Homework #2 is in, you guessed it, eCampus