CS 40 - Lecture 8

David S. Taylor
Lecture 7 Recap

- variable: holds a value
- initializing the variable: setting the first value
- instance variable: defined in object
- Local variable: defined in method
Reminder: Object

- an entity that can be manipulated in the program
- current state (color, position, texture, etc.) stored in instance
variables
- methods used to change state
- ex: toaster
Class Concept
- Class = set of objects with the same behavior
- Can have toaster, toaster2, toaster3 in the same program

- All belong to the same class
- Alice has hundreds of classes (Toaster, Hampster, Woman, etc. etc.)
- Fundamental operation: Make new instance of class (ex: add
another toaster)
Object vs. Class

- Object = one instance, Class = set of all instances
- Alice doesn't always make this very clear (ex. Cleopatra)
- Isn't there just one Cleopatra
- Actually, you can populate a world with lots of them
- A better name might have been EgyptianQueen
Defining New Classes in Alice

- Alice makes this very simple
- Make an object
- Add instance variables
- Add methods
- Get new behavior
- Example: ComboLock
Defining New Classes - Cont'd
- Added instance variables, methods, to comboLock
- Have new behavior
- Do we have a new class?
- Can we make two instances?
- Not yet, need to save as new class
Recipe for Saving Class

- Add instance variables, methods
- Rename object (e.g. smartComboLock)
- Right click, save object...
- Accept file name SmartComboLock.a2c (note uppercase S)
- To make another object of this class, use menu item
File->Import
Lab
- Download the ComboLock solution lab07.a2w
- Rename the object to smartComboLock
- Make it look smarter too. Give it a different color or texture. (To get
more texture choices, add some animals into the world.)
- Following the recipe for saving class, make two instances of
SmartComboLock
- How do you know you got smart combo locks, not the regular ones? Answer
in ActiveLecture.org
Reminders

- Submit any questions about the lab to GoogleGroups (no Alice
world upload needed)
- Homework #3 is due Sunday
- Exam #1 is next Wednesday
- I will try to get you some Homework feedback for HW 1 and 2 on Thursday.
- Is your photo in GoogleGroups? If not, please put it there.