430 likes | 696 Vues
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.
E N D
Object-Oriented Programming in PythonGoldwasser and LetscherChapter 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.
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.
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.
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)
Euclid's GCDValues for u, v, and r starting with u = 54 and v = 42. Answer is 6
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
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.
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.
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.
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.
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
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( )
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.
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.
Instance Methods • Kinds of methods: • Accessors or inspectors: method that returns the value of an instance attribute. • Mutators: change value of instance attribute. • Others
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.
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.
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.
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.
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.
Sequence Diagram for Student Registration System (1st Attempt)
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.
Drawable Class Diagram • Next slide shows a drawable class diagram. • Attributes: • depth. • transformation. • reference point. • Methods: • rotate. • move. • see others on diagram.