cosc 1p02
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
COSC 1P02
Intro. to Computer Science 8.1
Cosc 1P02
Only those who attempt the absurd can achieve the impossible.
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.9
Implementation—Payroll Class
Streams Processing algorithm
process to EOF report generation prompting detail line
Testing & debugging test scripts employee data file
boundary conditions
COSC 1P02
Intro. to Computer Science 8.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
COSC 1P02
Intro. to Computer Science 8.26
The end