knerr cs21 notes...

back to schedule

WEEK04: computer graphics, using objects
---------------------------------------------------------------
 M: what is an object? simple computer graphics

LAB3: due Tuesday
QUIZ 2 this Friday

- data types we've seen so far: int, float, string, boolean
- each has certain kinds of data/values and operations

- functions we've seen so far: type(5), len("hello"), range(10)

- objects combine data and functions (methods) into one thing

RADIO EXAMPLE:

 - for a real radio, you can turn it on/off, change the
   volume, change the station

 - if you wrote a program to act like a radio (like Pandora)
   you would want similar features:

     myradio = Radio()        <--- constructor creates object
                                   with initial data and methods
 - use dot operator to change the object data (manipulate the
   object)

     myradio.setVolume(10)
     myradio.setStation(91.5)
     
     anotherradio = Radio()
     anotherradio.setStation(90.1)

     ==> call object methods to change the object


COMPUTER GRAPHICS:

 - we are using the graphics module from the Zelle book

   from graphics import *

 - in the graphics window, x increases to the right, y increases
   down, so the point 0,0 is the top left corner

 - try some simple objects: GraphWin, Point, Line, Circle:

>>> from graphics import *
>>> w = GraphWin()
>>> p = Point(50,90)
>>> p.draw(w)
>>> c = Circle(p, 20)
>>> c.setFill("red")
>>> c.draw(w)

OBJECT-ORIENTED PROGRAMMING

  - build complex systems out of simple objects
  - more intuitive (circle object, fill, draw), objects in code
    correspond to real-world objects
  - reusability (design objects that can be used in other programs)

  - we'll design and create our own objects later this semester

YOUR TURN

 - see if you can write programs to make these images (use
   /home/jk/inclass/firstgraphics.py as a starting point):