110 likes | 262 Views
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
E N D
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 • things objects can do • behaviour • System execution via interacting objects
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
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
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
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
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
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
Implementation—Payroll Class • Streams • Processing algorithm • process to EOF • report generation • prompting • detail line • Testing & debugging • test scripts • employee data file • boundary conditions
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