CS 40 - Lecture 8
Cay S. Horstmann
Lecture 7 Recap
- variable: holds a value
- initializing the variable: setting the first value
- instance variable: defined in object
- Local variable: defined in method
How Was Homework #2?
ActiveLecture.org
- Easier than homework #1
- About the same difficulty as homework #1
- Harder than homework #1
- Not done yet
- What homework?
Thanks for your feedback!
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 (from
schedule)
- 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 lab07.a2w in eCampus
- Homework #2 is due tonight
- Homework #3 is due next Tuesday
- Exam #1 is next Thursday
- Is your photo in eCampus? If not, please put it there.