knerr cs21 notes...
back to schedule
WEEK11: defining our own classes/object-oriented programming
---------------------------------------------------------------
W: more classes
QUIZ 5 RETURNED:
- see /home/jk/inclass/q5.py and make sure you understand it!
CARD and DECK classes:
- see /home/jk/inclass/blackjack.py for an example of a program
that uses the card and deck classes
WORKSHEET:
- see /home/jk/inclass/worksheet.planetclass for a review of terms
and another example of a class definition
- in the Planet class, each planet object has a name, mass, radius,
(x,y) position, and (vx,vy) velocity. This class could be used in
a program to simulate planet motions in a solar system...
YOUR TURN:
- suppose we're writing a program for a bank. The bank needs to keep
track of all of it's customer accounts. Let's write an Account class!
- what type of data could be associated with each account???
name
address
phone number
account id number
balance
PIN number
- what, in our bank account program, might we want to do with
an account (what methods should we write for this class??)??
__init__ all classes need a constructor
__str__ would be nice to print out the account info
accessors getName, getPin, getBalance, etc
mutators changePin, changeBalance (withdraw, deposit), changeName, etc
Let's focus on just these 4 pieces of data:
name, account number, pin, balance
*** write an account.py file that defines a new Account class.
Here is some test code that your class should handle:
if __name__ == '__main__':
a1 = Account("Tia Newhall", "3456789", "1234", 2000.50)
print a1
a2 = Account("Jeff Knerr", "7891011", "5678", 49590.37)
print a2
print a2.getName()
print a2.getBalance()