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

review questions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

1

Aaron Stevens

11 February 2009

CS108 Lecture 11: Objects and Graphics

Objects and Classes Graphics Concepts

2

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.

slide-2
SLIDE 2

2

3

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

  • f a 2002 Honda Civic…

4

Some Theory

Our example manipulated several different kinds

  • f 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.

slide-3
SLIDE 3

3

5

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.

6

Creating an instance of a class

Often, the constructor is used as the right side

  • f an assignment statement, so the general form

becomes:

<var> = <class-name>(<param1>, <param2>,…)

Example:

p = Point(50,50)

slide-4
SLIDE 4

4

7

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)

8

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)

slide-5
SLIDE 5

5

9

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.

10

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()

slide-6
SLIDE 6

6

11

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)

12

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)

slide-7
SLIDE 7

7

13

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(‘ ‘)

14

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)

slide-8
SLIDE 8

8

15

Copying objects

Shallow Copy

Creates a new variable referring to the same

  • bject.

Deep Copy

Creates a copy of the object, so that now you have 2 objects.

16

Take-Away Points

– Objects – Classes – Constructors – Methods, parameters – RGB color codes – Shallow/deep copy

slide-9
SLIDE 9

9

17

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).