WEEK05: computer graphics, using objects --------------------------------------------------------------- M: what is an object? simple computer graphics LAB 4: due Saturday QUIZ 2 this Wednesday REVIEW from last week: str methods >>> help(str) >>> >>> S = "we love computer science" >>> S.upper() 'WE LOVE COMPUTER SCIENCE' >>> S.capitalize() 'We love computer science' >>> print S we love computer science (upper() and many str methods DON'T CHANGE the string S!!!) >>> S.split() ['we', 'love', 'computer', 'science'] >>> S.count("e") 5 >>> S.index("w") 0 there are many useful str methods!!! OBJECTS AND OOP (Object-Oriented Programming) - 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 SUDOKU EXAMPLE: puzzle = [9,0,7,5,6,0...] row1 = puzzle[0:9] col1 = puzzle[0:81:9] - can write sudoku program using above, OR we can create a Sudoku object that has getRow() and getCol() methods: puzzle = Sudoku([9,0,7,5,6,0...]) row1 = puzzle.getRow(1) col1 = puzzle.getCol(1) - using objects and methods: OBJECT.METHOD() 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 0,0 ------> x 500,0 | | | V y 0,300 500,300 - 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 a program to make this image: