1 / 11

Cosc 1P02

Cosc 1P02. Only those who attempt the absurd can achieve the impossible. Classes. Classes entities within a system imported e.g. Turtle of same project write more than one class Instances objects created via new Instance variables object’s memory state Methods

osma
Download Presentation

Cosc 1P02

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. Cosc 1P02 Only those who attempt the absurd can achieve the impossible.

  2. Classes • Classes • entities within a system • imported • e.g. Turtle • of same project • write more than one class • Instances • objects created via new • Instance variables • object’s memory • state • Methods • things objects can do • behaviour • System execution via interacting objects

  3. Methods • Method calls • local • methodname ( paramlist ) • of another object • object . methodname ( paramlist ) • this . methodname ( paramlist ) • Behaviour depends on state • e.g. effect of forward depends on pen state and direction • methods refer to instance variables • can base actions on values of instance variables • loops, decisions • Role of constructor • well-defined initial state

  4. Data Abstraction • Procedural abstraction is limited • Abstraction based on set of values and operations upon them • i.e. a class • many possible instances • each can perform methods • each object’s behaviour depends on its state • Don’t need to know how state recorded or how operations implemented to use object • Example: Turtle

  5. Case Study: Payroll System • Problem • weekly payroll • Analysis & design • entities • employee • details about employee • payroll system itself • report generation • basic algorithm • merger of report generation and process to EOF • encapsulate details about employee in Employee class • pay rate • technique to compute net pay • revised report algorithm • calculatePay • employee data

  6. Implementation—Employee Class • Class • not a main class • no method main • imports • Execution • only when methods (constructor) called • Attributes/instance variables • employee number, name • pay rate • year-to-date values

  7. Implementation—Employee Class. • Algorithms/methods • constructor • read data of one employee • calculatePay • references instance variables • references local methods • Memory model • instance variables • parameters • local variables

  8. Implementation—Employee Class. • Reading employee records • constructor • initial state • stream parameter • end of file • process to EOF pattern • Garbage collection • multiple Employee objects • garbage • Writing employee records • who knows employee data? • stream parameter • consistent with constructor

  9. Implementation—Payroll Class • Streams • Processing algorithm • process to EOF • report generation • prompting • detail line • Testing & debugging • test scripts • employee data file • boundary conditions

  10. Information Hiding • Class design • Cohesion • Selective disclosure • only what client needs to know • hide representation and implementation • Accessor and updater methods • instance variables • private • if accessible, provide accessor method • Java convention: getxxx • if updateable, provide updater method • Java convention setxxx • pseudo attributes • Methods • private vs public

  11. The end

More Related