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

QUIZ5 review: see q5.py file

OBJECTS REVIEW:

 - we've used objects already:

       cenpt = Point(200,200)
       c1 = Circle(cenpt, 100)
       c1.setFill("blue")
       c1.draw(win)

   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)


FIRST EXAMPLE of WRITING A NEW CLASS:

 - if we were going to write a game program that shows raindrops
   falling, that the user has to click on...

 - see /home/jk/inclass/raindrop.py for Raindrop class

 - terms/syntax/definitions...

       __init__     the constructor method -- called when we create
                    a new variable of this type (ex: d = Raindrop(win, 5))
       __str__      the print method -- called when we print a
                    variable of this type (ex: print d). 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
                    d.move(), self = d)
       accessors    methods that get or access the object data (like getValue())
       mutator      methods that change object data (like changeSpeed)

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 raindrop.py"),
                                   then this test code *will* be executed.

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

YOUR TURN:

 - try the raindrop class in the python shell:

$ python
>>> from raindrop import *
>>> from graphics import *
>>> gw = GraphWin("raindrops", 400, 700)
>>> gw.setBackground("grey20")
>>> d = Raindrop(gw, 5)

(note: at this point raindrop is hard to see because it's at top of screen)

>>> d.move()
>>> d.move()
>>> d.move()
>>> d.move()
>>> for i in range(20):
...   d.move()
... 
>>> 
>>> print d
raindrop   x: 302       y: 100
          dx:   0      dy:   5
       value:   5  radius:  20
>>> d.getValue()
5


WHY ARE WE DOING THIS???

 - 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