review questions
play

Review/Questions What are objects, and how do they help us program - PDF document

CS108 Lecture 11: Objects and Graphics Objects and Classes Graphics Concepts Aaron Stevens 11 February 2009 1 Review/Questions What are objects, and how do they help us program more effectively? Constructors: How to create


  1. CS108 Lecture 11: Objects and Graphics Objects and Classes Graphics Concepts Aaron Stevens 11 February 2009 1 Review/Questions – What are objects, and how do they help us program more effectively? – Constructors: How to create objects… – Methods: Telling objects to do stuff. 2 1

  2. Classes and Objects An analogy using cars: Consider a 2002 Honda Civic LX 4-door 5-speed, green exterior, grey interior, with AC. What does this describe, really? Can you drive it? What kind of condition is it in? – This actually describes an engineering specification from which an entire class of identical cars could be created. Now consider my wife’s car… what is it, really? – This describes a single instance of a vehicle -- made to the specification of a 2002 Honda Civic… 3 Some Theory Our example manipulated several different kinds of objects: Point, Line, Circle, Rectangle, Oval, Text These are examples of classes . Every object is an instance of a class , and the class describes the properties (data and behaviors) the object will have. 4 2

  3. Creating an instance of a class To create an instance of a class, we must use a special operation called a constructor. The general form for a constructor is: <class-name>(<param1>, <param2>,…) <class-name> is the name of the class of which to create an object instance (i.e. Point ). The number and type of parameters depend on the class. 5 Creating an instance of a class Often, the constructor is used as the right side of an assignment statement, so the general form becomes: <var> = <class-name>(<param1>, <param2>,…) Example: p = Point(50,50) 6 3

  4. Calling Methods To perform an operation on an object, we invoke (call) a method on that object. To invoke a method we use the dot notation: <object>.<method-name>(<param1>, <param2>,…) Example: p.draw(win) 7 Calling Methods To invoke a method we use the dot notation: <object>.<method-name>(<param1>, <param2>,…) More Examples: >>> circ.draw(win) >>> circ.setFill("green") >>> circ.move(10,10) 8 4

  5. A Note About Calling Methods Methods are very finicky about their parameters. When calling a method, be sure to: – Provide the correct number of parameters. – Provide the correct types of parameters. – Provide the correct order of parameters. The API reference page shows you the list of parameters required/expected for a given method. 9 Calling Methods Some methods do not take any parameters. These either return data, or else change the state of the object: More Examples: >>> p.getX() >>> p.getY() >>> circ.undraw() 10 5

  6. Anonymous Objects Some times we need an object only for an instant, and never again. In those cases, we can skip the step of assigning the object to a variable: >>> circ = Circle(Point(50,50),50) >>> circ.setFill(“blue”) >>> circ.draw(win) 11 Custom Colors RGB color model Computers represent colors by their red, green, and blue subcomponents. Custom colors can be created by “mixing” these colors. >>> r = Rectangle(Point(10,10),Point(50,50)) >>> r.draw(win) >>> purple = color_rgb(147,0,222) >>> r.setFill(purple) 12 6

  7. Another Object/Method Example Recall the python string module we used last week? Turns out, string is an object in Python; we can use <object>.<method> notation to call up its methods. Examples: s = “hello, world” s.upper() s.find(‘ ‘) 13 Copying objects >>> circ = Circle(Point(50,50),50) >>> circ.setFill(“blue”) >>> circ.draw(win) >>> other = circ # make a copy >>> other.move(100,0) # what happened here? >>> other.draw(win) 14 7

  8. Copying objects Shallow Copy Creates a new variable referring to the same object. Deep Copy Creates a copy of the object, so that now you have 2 objects. 15 Take-Away Points – Objects – Classes – Constructors – Methods, parameters – RGB color codes – Shallow/deep copy 16 8

  9. Student To Dos – Quiz 2 is on Friday 2/13  Covers lectures 5-9 (repetition, accumulator pattern, strings, files) – Reading:  chapter 5 (graphics)  Chapter 6 (Friday and next week) – Download the graphics.py module from http://mcsp.wartburg.edu/zelle/python/graphics.py, and save it in your code folder (with your examples). 17 9

Recommend


More recommend