Object_Oriented Programming in Python

advertisement
Object-Oriented Programming in Python
Goldwasser and Letscher
Chapter 1:
Cornerstones of Computing
Terry Scott
University of Northern Colorado
2007 Prentice Hall
Introduction: Chapter 1 Topics
• Data and types.
• Operations, Functions, and Algorithms.
• Two algorithms for determining the
greatest common divisor (GCD).
• High-level programming languages.
• Object-oriented paradigm.
• Designing and modeling.
2
Data and Types
• Information is organized data
• Data Types
– Built-in types sometimes called primitive
types: most languages have
• numbers,
• characters,
• lists/arrays.
– User-defined types:created by programmer.
3
Operations, Functions, and
Algorithms
• Central Processing Unit (CPU) – in charge
of the computer. Has limited set of
instructions.
• Control structures – built into a language
to control the order of instruction
execution.
• Function – high level behavior created by
the programmer.
4
Operations, Functions, and
Algorithms - Continued
• Abstraction – Functions allow programmer
to encapsulate some operation into a
chunk of code.
• Algorithms - step-by-step instructions for
solving a problem.
– Flowchart – graphical display of an algorithm.
– Algorithm for finding Greatest Common
Divisor (GCD)
5
GCD: Algorithm
6
GCD Algorithm (Euclid)
7
Euclid's GCD
Values for u, v, and r starting with u = 54 and v = 42.
Answer is 6
u
v
r
54
42
12
42
12
6
12
6
0
6
0
8
High Level Programming
Languages
• Kinds of Languages.
– Low-level programming language:
• Machine code.
• Assembly code.
– High-level code:
• Python
• C++
• Many others
• Source Code – code written by
programmer that is converted into
executable code by compiler or interpreter 9
High Level Programming
Languages (continued)
• Compiler versus Interpreter
– Compiler generates an executable file that
can be run on the computer
– Interpreter generates machine code and
executes it immediately one line at a time.
• Syntax versus Semantics
– Syntax: rules followed by a language.
– Semantics: meaning of statements in a
language.
10
High Level Programming
Languages (continued)
• Syntax error: when rules of a language are
violated. These are found by the compiler
or interpreter.
• Semantic error (logic error). Errors not
found by the computer.
• Semantic errors lead to incorrect results.
11
Object-Oriented Paradigm
• Object-oriented programming (OOP)
• Objects and Classes.
– objects are created from classes.
– single object from a class is called an
instance.
– Data and operations on that data are
encapsulated together. Unit called a class.
12
Objects
• Data within a class is called an attribute.
• All attributes together represent the state
of an instance.
• Operations contained in a class are called
methods.
13
Obedient Dog Sequence Diagram
• This and the next slide refer to a diagram
two slides ahead.
• Jane and Spot are instances of Person
and Dog
• Vertical lines represent chronological
lifetime of the instances.
• Solid horizontal lines represent flow of
control passing from Jane to spot
14
Obedient Dog Sequence Diagram
• Rectangular boxes under Spot represent time
passing while operation is being performed.
• Dotted horizontal lines indicate control being
passed back to Jane.
• Methods are:
–
–
–
–
sit( )
liedown( )
rollover( )
fetch( )
15
Obedient Dog: Sequence Diagram
16
Obedient Dog Fetching Slippers
• Next slide shows a parameter being
passed to the fetch() method
• The fetch method expects a parameter
telling what to fetch (slippers)
• The dotted horizontal line indicates that
redSlippers have been fetched.
17
jane invokes: spot.fetch(slippers)
18
Instance Methods
• Parameter – how information is passed
into a method.
• Return value – how information is passed
out of a method.
• The parameters and return value of a
method are called its signature.
19
Instance Methods
• Kinds of methods:
– Accessors or inspectors: method that returns
the value of an instance attribute.
– Mutators: change value of instance attribute.
– Others
20
Television Class
• Diagram on next slide is for a Television
class.
• In general these are called class
diagrams.
• Upper rectangle lists the attributes of the
class
• Lower rectangle lists the methods for the
class.
21
Television Diagram: Attributes and
Methods
22
Television Class Design: Methods
• togglePower(): method toggles a switch –
pressing flips between on and off.
• toggleMute(): method toggles a switch. TV
must be on to have it toggle.
• volumeUp(): changes volume when TV is
on and only goes until some maximum
volume.
23
Television Class Design: Methods
(continued)
• volumeDown(): changes volume
when TV is on and only goes until
some minimum volume.
• channelUp(): changes channel when
TV is on and wraps around to lowest
channel once the highest channel is
reached.
• channelDown() same as channel up
but wraps from low channel to high.
24
Television Class Design: Methods
(continued)
• setChannel(number): changes channel to
number if TV is on.
• jumpPrevChan() changes channel to
previous channel if TV is on.
25
Student Registration System
• Has-a relationships: combination of
objects.
• Is-a relationships: inheritance between
objects.
• Bob has-a schedule: called composition.
• Design of a student registration system:
sequence diagrams on next slides.
26
Sequence Diagram for Student
Registration System (1st Attempt)
27
Student Registration System
Sequence Diagram (2nd Attempt)
28
Student Registration System
Sequence Diagram (3rd Attempt)
29
Student Registration System
Sequence Diagram (4th Attempt)
30
Class Diagrams for Independent
Student and Professor
31
Student and Professor Inherit
from Person
• Student and Professor are both persons.
• Common attributes:
– name.
– birthdate.
– phone number.
– current Schedule.
• Student is-a Person and Professor is-a
Person.
• Student and Professor inherit from Person.
32
Student and Professor Classes
Derived from Person
33
Drawable Class Diagram
• Next slide shows a drawable class
diagram.
• Attributes:
– depth.
– transformation.
– reference point.
• Methods:
– rotate.
– move.
– see others on diagram.
34
Drawing Package: Class Diagram
35
Proposed Hierarchy of Drawable
Objects Showing Inheritance.
36
Download