WEEK12: defining our own classes/object-oriented programming
 M: classes

QUIZ6 will be next week!
Lab 10 is due this Thursday
Lab 11 will be due next Tuesday


 - we've used objects already:

       cenpt = Point(200,200)
       c1 = Circle(cenpt, 100)

   in the above code, cenpt is a Point object, and c1 is a Circle
   object. We also used the Circle class' methods setFill and draw

 - an object is just data *and* functions all together in one "thing"

 - for the above Circle object, the data are the center point, the
   radius (100), and the color. The functions are just the methods
   that do something to the circle (fill it, draw it, move it)

 - in Python, almost everything is an object. Look at the help info
   for str and list and you can see all of their methods:

>>> help(str)

>>> help(list)


 - think back to our typingTutor game, with letters falling
   and the user having to type them before they hit the bottom
   of the screen

 - see /home/jk/inclass/letter.py for the Letter class

 - terms/syntax/definitions...

       __init__     the constructor method -- called when we create
                    a new variable of this type (ex: L = Letter(win))
       __str__      the print method -- called when we print a
                    variable of this type (ex: print L). This method must
                    return a string!
       self         the first parameter to any method. This is just a reference
                    to the object on which the method is acting (if I say
                    L.move(), self = L)
       accessors    methods that get or access the object data (like getChar())
       mutator      methods that change object data (like a changeSpeed() method)

if __name__ == '__main__':         this line is for testing your new class. if your
  # add your test code here        new class is imported, this code will not be run.
                                   if your class is run directly (like "python letter.py"),
                                   then this test code *will* be executed.

"""triple-quote comments"""        also note the use of the triple-quoted comments in letter.py!
                                   if you say "import letter" and then "help(letter)" the documentation
                                   you see *is* the triple-quoted comments!!


 - try the letter class in the python shell:

$ python
>>> from letter import *
>>> from graphics import *
>>> gw = GraphWin("letters", 400, 700)
>>> gw.setBackground("grey20")
>>> L = Letter(gw)

>>> L.move()
>>> L.move()
>>> L.move()
>>> L.move()
>>> for i in range(20):
...   L.move()
>>> print L
>>> print L
R at (114,20)
>>> L.getChar()


 - object-oriented programming (OOP) is a different way to program. Some
   think it is better (easier to maintain, cleaner) for large, complicated programs.

 - if you write a good general class, it's easy to use it over and over
   in many programs (code reuse == good)

 - some think OOP is more intuitive, since the objects you create usually
   represent real-life objects (cards, planets, students, etc)

 - INTERFACE between class writer and program writer


 - suppose we wanted different letters to fall at different speeds?
   can you add a speed for each letter? How would we have to do this
   in the old typingTutor.py game, without using the Letter class?

 - suppose we want the letters to speed up as the game goes on? Can
   you add a changeSpeed() method to the class?


 - copy /home/jk/inclass/worksheet.planetclass and see if you can answer
   the questions at the bottom for next time