Python Programming: An Introduction to Computer Science - PowerPoint PPT Presentation

About This Presentation
Title:

Python Programming: An Introduction to Computer Science

Description:

Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Python Programming, 2/e* – PowerPoint PPT presentation

Number of Views:277
Avg rating:3.0/5.0
Slides: 27
Provided by: TerryL56
Category:

less

Transcript and Presenter's Notes

Title: Python Programming: An Introduction to Computer Science


1
Python ProgrammingAn Introduction toComputer
Science
  • Chapter 4
  • Objects and Graphics

2
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
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 its manipulated and combined with
    active operations

4
Overview
  • Modern computer programs are built using an
    object-oriented approach
  • Most applications youre familiar with have
    Graphical User Interfaces (GUI) that provide
    windows, icons, buttons and menus
  • Theres a graphics library (graphics.py) written
    specifically to go with this book. Its based on
    Tkinter

5
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
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
The Object of Objects
  • The student object would contain data like
  • Name
  • ID number
  • Courses taken
  • Campus Address
  • Home Address
  • GPA
  • Etc.

8
The Object of Objects
  • The student object should also respond to
    requests.
  • We may want to send out a campus-wide mailing, so
    wed 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
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
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
  • 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()

12
Other str operations
  • http//docs.python.org/library/stdtypes.htmlstrin
    g-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
Simple Graphics Programming
  • This chapter uses the graphics.py library
    supplied with the supplemental materials
  • Two location choices
  • In Pythons Lib directory with other libraries
  • In the same folder as your graphics program
    (what we'll do)
  • like copying Rick's test modules

14
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 appearwin GraphWin()
  • What type of object is win?

15
Simple Graphics Programming
  • Windows can be closed/destroyed by issuing the
    commandwin.close()

16
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
Simple Graphics Programming
-10- -20- -30- -40- -50- -60- -70- -80- -9
0- -100- -120- -130- -140--150- -160- -170
  • 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)

18
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
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
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
Using Graphical Objects
  • To create a new instance of a class, we use a
    special operation called a constructorltclass-name
    gt(ltparam1gt, ltparam2gt, )
  • ltclass-namegt 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
Using Graphical Objects
  • Only the most relevant instance variables are
    shown (others include the color, window they
    belong to, etc.)

23
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-notationltobjectgt.lt
    method-namegt(ltparam1gt, ltparam2gt, )
  • 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
Circle remembers its Point
  • circ Circle(Point(100, 100), 30)win
    GraphWin()circ.draw(win)

25
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 gt width ______
  • What color is rect's border? ______
  • How many pixels are yellow if the border
  • is 1 pixel wide? ______

26
The GraphWin object
Write a Comment
User Comments (0)
About PowerShow.com