Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing

Post on 18-Jan-2016

41 views 1 download

Tags:

description

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. - PowerPoint PPT Presentation

Transcript of Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing

Object-Oriented Programming in PythonGoldwasser and Letscher

Chapter 1:Cornerstones of Computing

Terry Scott

University of Northern Colorado2007 Prentice Hall

2

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.

3

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.

4

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.

5

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)

6

GCD: Algorithm

7

GCD Algorithm (Euclid)

8

Euclid's GCDValues 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

9

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

10

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.

11

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.

12

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.

13

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.

14

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

15

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

16

Obedient Dog: Sequence Diagram

17

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.

18

jane invokes: spot.fetch(slippers)

19

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.

20

Instance Methods

• Kinds of methods:– Accessors or inspectors: method that returns

the value of an instance attribute.– Mutators: change value of instance attribute.– Others

21

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.

22

Television Diagram: Attributes and Methods

23

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.

24

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.

25

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.

26

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.

27

Sequence Diagram for Student Registration System (1st Attempt)

28

Student Registration System Sequence Diagram (2nd Attempt)

29

Student Registration System Sequence Diagram (3rd Attempt)

30

Student Registration System Sequence Diagram (4th Attempt)

31

Class Diagrams for Independent Student and Professor

32

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.

33

Student and Professor Classes Derived from Person

34

Drawable Class Diagram

• Next slide shows a drawable class diagram.

• Attributes:– depth.– transformation.– reference point.

• Methods:– rotate.– move.– see others on diagram.

35

Drawing Package: Class Diagram

36

Proposed Hierarchy of Drawable Objects Showing Inheritance.