object-oriented programming in python goldwasser and letscher chapter 1: cornerstones of computing

36
Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing Terry Scott University of Northern Colorado 2007 Prentice Hall

Upload: jorn

Post on 18-Jan-2016

45 views

Category:

Documents


1 download

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

Page 1: 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

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

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.

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

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.

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

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.

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

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)

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

6

GCD: Algorithm

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

7

GCD Algorithm (Euclid)

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

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

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

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

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

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.

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

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.

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

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.

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

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.

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

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

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

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

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

16

Obedient Dog: Sequence Diagram

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

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.

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

18

jane invokes: spot.fetch(slippers)

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

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.

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

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

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

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.

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

22

Television Diagram: Attributes and Methods

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

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.

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

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.

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

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.

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

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.

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

27

Sequence Diagram for Student Registration System (1st Attempt)

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

28

Student Registration System Sequence Diagram (2nd Attempt)

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

29

Student Registration System Sequence Diagram (3rd Attempt)

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

30

Student Registration System Sequence Diagram (4th Attempt)

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

31

Class Diagrams for Independent Student and Professor

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

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.

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

33

Student and Professor Classes Derived from Person

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

34

Drawable Class Diagram

• Next slide shows a drawable class diagram.

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

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

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

35

Drawing Package: Class Diagram

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

36

Proposed Hierarchy of Drawable Objects Showing Inheritance.