Wes Griffin & Sue Evans
Hit the space bar for next slide
The classic style of computer programming is called procedural programming.
>>> x = 3.5 >>> y = 4.7 >>> print x * y
>>> len("spam") >>> ord('a')
There is a more recent style of computer programming called object-oriented programming.
Imagine a program to manage an ATM. Each account the ATM knows about is an object. The account objects hold pieces of data and respond to certain messages.
>>> acct1 = BankAccount("Betsy Horner", 1000) >>> acct2 = BankAccount("Jordan Wales", 675)
>>> acct1.getBalance() 1000 >>> acct1.makeDeposit(500) >>> acct1.getBalance() 1500 >>> acct2.makeWithdrawal(200) >>> acct2.getBalance() 475
>>> from graphics import *
>>> window = GraphWin()
>>> window.close()
The GraphWin constructor takes three parameters: title, width, and height:
>>> GraphWin(<title>, <width>, <height>)
When the parameters are not specified, they have the following default values:
>>> window = GraphWin() >>> window.width 200
>>> newWindow = GraphWin("My Window", 500, 500) >>> newWindow.width 500
>>> window.setBackground("red")
>>> newWindow.setBackground("purple")
>>> p = Point(50, 60)
>>> q = Point(100, 300)
>>> q = pthen the q variable now references the object pointed to by p:
>>> circ = Circle(Point(100, 300), 30)
>>> circ.draw(newWindow)
>>> line = Line(Point(50, 100), Point(175, 325)) >>> line.draw(newWindow)
from graphics import * def main(): win = GraphWin("Tic-Tac-Toe", 400, 400) # set coordinates to go from (0,0) in upper left to (3,3) in lower right win.setCoords(0.0, 0.0, 3.0, 3.0) # draw vertical lines Line(Point(1, 0), Point(1, 3)).draw(win) Line(Point(2, 0), Point(2, 3)).draw(win) # draw horizontal lines Line(Point(0, 1), Point(3, 1)).draw(win) Line(Point(0, 2), Point(3, 2)).draw(win) # wait for a mouse click before closing window win.getMouse() win.close() main()