knerr cs21 notes...

back to schedule

WEEK11: defining our own classes/object-oriented programming
 W: more classes


  - see /home/jk/inclass/ and make sure you understand it!

CARD and DECK classes:

  - see /home/jk/inclass/ for an example of a program
    that uses the card and deck classes


  - 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...


  - 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???

    phone number
    account id number
    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 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()