Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.

Similar presentations


Presentation on theme: "Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics."— Presentation transcript:

1 Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics

2 Python Programming, 2/e2 Objectives To understand the concept of objects and how they can be used to simplify programs To be familiar with some objects available in Zelle's graphics library To be able to create objects in programs and call appropriate methods to perform graphical computations

3 Python Programming, 2/e3 Overview Each data type can represent a certain set of values, and each had a set of associated operations The traditional programming view is that data is passive – it ’ s manipulated and combined with active operations

4 Python Programming, 2/e4 Overview Modern computer programs are built using an object-oriented approach Most applications you ’ re familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus There ’ s a graphics library (graphics.py) written specifically to go with this book. It ’ s based on Tkinter

5 Python Programming, 2/e5 The Object of Objects Basic idea – view a complex system as the interaction of simpler objects. An object is a sort of active data type that combines data and operations Objects know stuff (contain data) and they can do stuff (have operations) Objects interact by sending each other messages

6 Python Programming, 2/e6 The Object of Objects Suppose we want to develop a data processing system for a college or university. We must keep records on students who attend the school. Each student will be represented as an object.

7 Python Programming, 2/e7 The Object of Objects The student object would contain data like: Name ID number Courses taken Campus Address Home Address GPA Etc.

8 Python Programming, 2/e8 The Object of Objects The student object should also respond to requests. We may want to send out a campus-wide mailing, so we ’ d need a campus address for each student. We could send the printCampusAddress message to each student object. When the student object receives the message, it prints its own address.

9 Python Programming, 2/e9 Object of Objects Objects may refer to other objects. Each course might be represented by an object: Instructor Student roster Prerequisite courses When and where the class meets

10 Python Programming, 2/e10 Sample Operations ista130 = Course("CESL 102") ista130.addStudent('Jessie') ista130.addStudent('Casey') ista130.removeStudent('Jessie') ista130.changeRoom("CESL103") print(ista130.numberOfStudents() ) # Guess the Output

11 str objects are immutable, cannot change them can be referenced with a variable aStrObject = str('a string object') have data (not seen) a sequence of characters an integer as current number of characters locale like U.S., Germany, or China,... have operations (methods) upped = aStrObject.upper() 11

12 Other str operations Python Programming, 2/e12 http://docs.python.org/library/stdtypes.html#string-methods s = str('Jessie Casey') # a str object s.count('e') # Value? s.index('ssi') # Value? s.index('Not') # Value? s.endswith('sey') # Value? s.split(' ') # Value?

13 Python Programming, 2/e13 Simple Graphics Programming This chapter uses the graphics.py library supplied with the supplemental materials Two location choices In Python ’ s Lib directory with other libraries In the same folder as your graphics program (what we'll do) like copying Rick's test modules

14 Python Programming, 2/e14 Simple Graphics Programming Import all functions with '*' and avoid using graphics. in graphics. graphWin() from graphics import * A graphics window is a place on the screen where the graphics will appear win = GraphWin() What type of object is win ?

15 Python Programming, 2/e15 Simple Graphics Programming Windows can be closed/destroyed by issuing the command win.close()

16 Python Programming, 2/e16 Simple Graphics Programming A graphics window is a collection of points called pixels (picture elements). The default GraphWin is 200 pixels tall by 200 pixels wide (40,000 pixels total). One way to get pictures into the window is one pixel at a time, which would be tedious. The graphics routine has a number of predefined routines to draw geometric shapes.

17 Python Programming, 2/e17 Simple Graphics Programming The simplest object is the Point. Like points in geometry, point locations are represented with a coordinate system (x, y), where x is the horizontal location of the point and y is the vertical location. The origin (0,0) in a graphics window is the upper left corner. X values increase from right to left, y values from top to bottom. Lower right corner is (199, 199) -10- -20- -30- -40- -50- -60- -70- -80- -90- -100- -120- -130- -140- -150- -160- -170

18 Python Programming, 2/e18 Point and Circle objects from graphics import * win = GraphWin() p = Point(50, 60) p.draw(win) p.getX() # Value? ____ p.getY() # Value? ____ circ = Circle(p, 20) circ.draw(win) # What is the size of the # radius in pixels? _____

19 Python Programming, 2/e19 Using Graphical Objects Computation is preformed by asking an object to carry out one of its operations In the previous examples we manipulated str, GraphWin, and Point These are examples of classes

20 Python Programming, 2/e20 Using Graphical Objects The class describes the properties of the instance: the data and operations We will write our own classes in chapter 10 Each object is an instance of some class, If we say that Augie is a dog, then Augie is a specific individual in the larger class of all dogs. Augie is an instance of the dog class.

21 Python Programming, 2/e21 Using Graphical Objects To create a new instance of a class, we use a special operation called a constructor (,, …) is the type of object s1 = str('Leslie') p = Point(50, 60) c = Circle(p, 20) The arguments become the data remembered by the object To create a new instance of a class, we use a

22 Python Programming, 2/e22 Using Graphical Objects Only the most relevant instance variables are shown (others include the color, window they belong to, etc.)

23 Python Programming, 2/e23 Using Graphical Objects To perform an operation on an object, we send the object a message. The set of messages an object responds to are called the methods of the object. Methods are functions that live inside the object. Methods are invoked using dot-notation:. (,, …) s.rjust(8,'*') # s is str object p.draw(win) # p is a Point object fromLeft = p.getX() c.setFill('yellow') # c is a Circle object

24 24 Circle remembers its Point circ = Circle(Point(100, 100), 30) win = GraphWin() circ.draw(win)

25 Python Programming, 2/e25 Rectangle Objects upperLeft = Point(50, 50) lowerRight = Point(150, 80) rect = Rectangle(upperLeft, lowerRight) rect.setFill('yellow') rect.setOutline('red') rect.draw(win) # Is rect's height > width ______ # What color is rect's border? ______ # How many pixels are yellow if the border # is 1 pixel wide? ______

26 Python Programming, 2/e26 The GraphWin object


Download ppt "Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics."

Similar presentations


Ads by Google