object-oriented programming in python goldwasser and letscher chapter 1: cornerstones of computing
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 PresentationTRANSCRIPT
![Page 1: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/6.jpg)
6
GCD: Algorithm
![Page 7: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/7.jpg)
7
GCD Algorithm (Euclid)
![Page 8: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/16.jpg)
16
Obedient Dog: Sequence Diagram
![Page 17: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/18.jpg)
18
jane invokes: spot.fetch(slippers)
![Page 19: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/22.jpg)
22
Television Diagram: Attributes and Methods
![Page 23: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/27.jpg)
27
Sequence Diagram for Student Registration System (1st Attempt)
![Page 28: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/28.jpg)
28
Student Registration System Sequence Diagram (2nd Attempt)
![Page 29: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/29.jpg)
29
Student Registration System Sequence Diagram (3rd Attempt)
![Page 30: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/30.jpg)
30
Student Registration System Sequence Diagram (4th Attempt)
![Page 31: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/31.jpg)
31
Class Diagrams for Independent Student and Professor
![Page 32: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/33.jpg)
33
Student and Professor Classes Derived from Person
![Page 34: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/35.jpg)
35
Drawing Package: Class Diagram
![Page 36: Object-Oriented Programming in Python Goldwasser and Letscher Chapter 1: Cornerstones of Computing](https://reader035.vdocuments.net/reader035/viewer/2022062423/56814d40550346895dba64d7/html5/thumbnails/36.jpg)
36
Proposed Hierarchy of Drawable Objects Showing Inheritance.