1 / 154

Introduction to Object-Oriented Programming and the Java Programming Language

Introduction to Object-Oriented Programming and the Java Programming Language. Session 1: Introduction to Object-Oriented Programming Objects, Messages And Classes. Session 1: Overview. O-O Roadmap Basic Concepts and Vocabulary (OOP-speak) Objects in an Executing Program

osias
Download Presentation

Introduction to Object-Oriented Programming and the Java Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction toObject-Oriented Programmingand theJava Programming Language

  2. Session 1:Introduction toObject-Oriented ProgrammingObjects, Messages And Classes

  3. Session 1: Overview • O-O Roadmap • Basic Concepts and Vocabulary (OOP-speak) • Objects in an Executing Program • Kindergarten Object-Oriented Design • Stock Portfolio Problem • Vending Machine Problem

  4. Characteristics of OOP • Programming as modeling • The model elements are called objects • An executing program is a collection of objects

  5. In Object-Oriented Programming... Objects are the active elements in the program. Not: “the computer does …” Rather: “the object does …”

  6. Objects and Messages • Objects are activated by messages • Objects act by sending messages • Senders and receivers

  7. What's In A Message? Name of Desired Behavior print (method name) Additional Information “Hello world” (arguments) print(“Hello world”)

  8. What's In An Object? Code To Implement Various Behaviors (methods) push, pop, isEmpty withdraw, deposit, getBalance Internal State (variables) arr, top accountNum, owner, balance, dateOpened

  9. argument1 argumentN method1 method2 method3 method4 Messages and Methods method1 An Object (sender) A Message: Method name + arguments An Object (receiver)

  10. method1 method2 method2 method2 method3 message argument1 method4 argumentN An Object (sender) An Object (receiver) Returned object When An Object Recieves A Message

  11. method1 argument1 … argumentN Behavior • Behavior: what an object does • Determined by object's methods • Instigated by receipt of message method1 method2 Object (receiver)

  12. Prototype Syntactic Description of • Message • Method name • Arguments accompanying objects in the message • Message result • an object that is returned to the sender

  13. Interface • Collection of prototypes • Interface of object: prototypes of the object's methods • (Java uses the term interface for a related but more language-specific concept)

  14. Classes • Class— a repository of behavior • Object: an instance of a class • Object’s behavior defined by its class • Instances of the same class share behavior

  15. State • Variables internal to object— instance variables • Modifies behavior of objects of the same class • Example: 2 Name objects w/ a getFirst method • Instances of the same class • Share behavior • Differ in state • Generally hidden, invisible

  16. get first get first get first get first get first get first Jackie Jones Name get first add title Joe Thurm Jackie Joe Yudy Gerry Weiss Instances and State

  17. Object Creation (I) • The Creation Problem: The object does not exist yet— can’t send it a message • Possible Solutions? • Implicit or explicit message to class (as if the class were an object!) • Special creator object • Not a real problem, just metaphysical!

  18. Object Initialization Who should take responsibility for initializing an object’s state upon creation? THE OBJECT ITSELF OF COURSE!

  19. Constructors • special methods of the object • invoked during creation only

  20. SomeClass SomeClass method1 method1 methodN methodN constructor constructor New Object New Object Object Creation: Summary Creating Object constructor

  21. Program Initiation • Similar to the Object Creation Problem • if no objects exist at the start, how does anything happen? • One Initiation Myth: • Special application object • External entity sends it a start message invoking a start method • Not a real problem, just metaphysical!

  22. Imperative Programming— Gone? • Lives in modified form in method definitions • assignment • control structures • message-sending • non-object data types (language dependent)

  23. Imperative ProgrammingUnder OOP: • The object, not the computer, is the actor • minor point perhaps? (I don’t think so!)

  24. O-O vs Procedural Programming (I) • Procedural: • if you want something done find a procedure and call it • If no procedure exists, write one

  25. O-O vs Procedural Programming (II) • OOP: • If you want something done find an object and send it a message • If no object exists, find a class and create an instance • If no class exists, write a class definition

  26. O-O vs Procedural Programming (III) • Procedural programmer spends life writing procedures • O-O programmer spends life writing class definitions

  27. Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of The Problem: Given a source of time card information and a personnel file, print payroll checks

  28. What Objects Would You Expect? Statement of The Problem (again): Given a source of time card information and a personnel file, print payroll checks Common heuristic: look for the nouns.

  29. PayrollMaster Employee PersonnelFile TimeCard TimeCardFile Check SS# Hours Payroll Classes

  30. Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Objects

  31. Program PersonnelFile PersonnelFile PersonnelFile main lookup lookup lookup TimeCard File TimeCard File TimeCard File getCard getCard getCard Payroll Master Payroll Master Payroll Master DoPayroll DoPayroll DoPayroll Payroll Program (1)

  32. PersonnelFile lookup TimeCard TimeCard TimeCard TimeCard File getTime getTime getTime getSS getSS getSS getCard Payroll Master DoPayroll PayrollMaster: DoPayroll (1)

  33. PersonnelFile lookup get SS get SS get SS get SS TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS SS SS SS match match match match PayrollMaster: DoPayroll (2)

  34. Time Time Time Time Time getHours getHours getHours getHours getHours PersonnelFile lookup get Time get Time get Time get Time TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS match PayrollMaster: DoPayroll (3)

  35. Time getHours PersonnelFile lookup PayYourself PayYourself PayYourself Employee Employee Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS getSS match Personnel: Lookup

  36. Time getHours PersonnelFile lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check Check Check Check Check Check getSS match print print print print print print PayrollMaster: DoPayroll (4)

  37. Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Program (2)

  38. SMART and HELPFUL Objects A Peculiar Employee Design? • Since when do employees create their own checks? • Since when are employees trusted to determine their own pay? (not in OUR contract!)

  39. BREAK! Coming Up: Object-Oriented Design

  40. OOP and O-O Design • Can't rush in and “program” • Design phase needed even in simple problems • desirable in procedural programming • sine qua non in O-O world • O-O design methodologies: • numerous • we'll consider just one simple one

  41. Statement of Problem Possible Objects Primary Object Behavior Interface Sample Use Implement Kindergarten OOD

  42. Kindergarten OOD • Not appropriate for all problems • Works surprisingly well on many • great for CS1 • Illustrate with an example: Find the value of a portfolio of stocks based on “ticker-tape” information

  43. Statement of Problem: Find the value of a portfolio of stocks based on "ticker-tape" information.

  44. Possible Objects: • Portfolio • Holding (a portfolio item) • Value • TickerTape

  45. Primary Object? • not Holding • not Value • Portfolio vs. TickerTape?

  46. Portfolio vs. TickerTape? • Both primary in the sense of independence • But to which object should we send a message to solve our problem?

  47. Is Portfolio the Primary Object? • If so, Portfolio has this behavior: get Value of Portfolio, given TickerTape

  48. Is TickerTape the Primary Object? • If so, TickerTape has this behavior: • get Value of Portfolio, given Portfolio • Should TickerTape be responsible for computing the Value of a Portfolio?

  49. Responsibility-Driven Design: • objects should be responsible for themselves • example: • objects should be responsible for their own initialization (constructors) • TickerTape should not be responsible for Value of a Portfolio, Portfolio should • Primary Object:Portfolio

  50. Behavior of Portfolio: • get Value, given TickerTape • constructor, given a file listing of a portfolio

More Related