Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible.

Similar presentations


Presentation on theme: "COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible."— Presentation transcript:

1 COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible.

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 COSC 1P02 Intro. to Computer Science 8.11 Payroll Report Header, in this case the writeHeader() will have no parameters. Detail line given by writeDetail(Employee anEmployee, double hours, double pay writeSummery(double totPay, double totYTDPay);

12 COSC 1P02 Intro. to Computer Science 8.12 Payroll Report.

13 COSC 1P02 Intro. to Computer Science 8.13 Sample Payroll Data File Employee Number Tax Emp Name Pay

14 COSC 1P02 Intro. to Computer Science 8.14 Memory Model for Payroll Program

15 COSC 1P02 Intro. to Computer Science 8.15 Payroll Report Algorithm.

16 COSC 1P02 Intro. to Computer Science 8.16 Prompter in Payroll System

17 COSC 1P02 Intro. to Computer Science 8.17 Payroll Problem Employee Data File Updated Employee Data File Payroll Program User inputs Hours that an Employee worked that week Payroll Report Happiness Messages

18 COSC 1P02 Intro. to Computer Science 8.18 Employee.. Employee 1111 12.50 10000.00 2300.00 Employee 2222 5.50 4400.00 0.00 Employee 3333 7.50 3000.00 0.00 Employee 4444 45.00 36000.00 8280.00 Each Employee has his own set of attributes (Data). We treat each employee the same, same calculations apply. Knowledge about how to manipulate the employee’s attributes can be packaged as an Employee Class. Employee Class Each individual Employee is then an instance of this class. “Employee Object”. Since we have four employees in the data file, four instances are created. Knowledge of how to calculate the pay is also part of the employee class Calculate an Employee’s Pay

19 COSC 1P02 Intro. to Computer Science 8.19 Employee Class public class Employee { private String empNum; // employee number private String empName; // employee name private double rate; // pay rate private double YTDPay; // year-to-date gross pay /** The constructor creates a new employee reading the employee **data from a file. ** **@paramfromdata file for employee data.*/ public Employee ( ASCIIDataFile from ) { empNum = from.readInt(); if ( !from.isEOF() ) { empName = from.readString(); rate = from.readDouble(); YTDPay = from.readDouble()}; };// constructor Instance variables correspond to the attributes which define an Employee The Data file which contains the information is passed to the constructor as a parameter. Allows the data file to be independent of the class Successful constructor execution is dependent on reading valid data.

20 COSC 1P02 Intro. to Computer Science 8.20 Employee Class. public double calculatePay ( double hours ) { double pay; // gross pay if ( hours > 40.0 ) { pay = 40.0 * rate + ( hours - 40.0 ) * rate * 1.5; } else { pay = hours * rate; } YTDPay = YTDPay + pay; return pay; }; // calculatePay Methods which can be accesse d from outside of the class are public.

21 COSC 1P02 Intro. to Computer Science 8.21 Processing Each Employee private void doMonthEnd ( ) { Employee anEmp; // the employee double hours; // hours worked double pay; // employee's pay double totPay; // total pay double totYTDPay; // total year-to-date pay int button; // button pressed buildForm(); setUpReport(new Date()); totPay = 0; totYTDPay = 0; while ( true ) { anEmp = new Employee(empData); if ( empData.isEOF() ) break; fillForm(anEmp); button = display.accept(); if ( button == 0 ) { hours = display.readDouble("hours"); pay = anEmp.calculatePay(hours); writeDetail(anEmp,hours,pay); totPay = totPay + pay; totYTDPay = totYTDPay + anEmp.getYTDPay(); }; anEmp.write(newEmpData); }; writeSummary(totPay,totYTDPay); empData.close(); newEmpData.close(); display.close(); report.close(); }; // doMonthEnd Holds the employee object Create the form, report and zero the variables to keep track of total pay and ytd pay. Try to create an employee. If the operation failed then assume EOF Emp object is passed to fill in the form. If we want to update the employee then process record, Note write Detail to the report. Write out record to new emp data file

22 COSC 1P02 Intro. to Computer Science 8.22 PayRoll fillform /** This method fills in the fields of the form from the Employee. */ private void fillForm ( Employee anEmployee ) { display.clearAll(); display.writeString("empNum",anEmployee.getEmpNum()); display.writeString("empName",anEmployee.getEmpName()); display.writeDouble("rate",anEmployee.getRate()); display.writeDouble("YTDPay",anEmployee.getYTDPay()); }; // fillForm Employee objects can get passed as parameters to methods Only Instance variables with accessor methods are available.

23 COSC 1P02 Intro. to Computer Science 8.23 Garbage Collection anEmployee Employee 1111 12.50 10000.00 2300.00 Employee 2222 5.50 4400.00 0.00 Employee 3333 7.50 3000.00 0.00 Employee 4444 45.00 36000.00 8280.00 Each time through the while loop a new object is created replacing what anEmployee references. Anything not explicitly referenced is not accessible, thus useless When the Java Virtual machine recognises that it is running out of resources (memory) it must reclaim memory which is of no use The garbage collection routine kicks in scanning for any object not referenced. When it finds such an object the memory is reclaimed. System Memory

24 COSC 1P02 Intro. to Computer Science 8.24 Writing Employee Record public void write ( ASCIIOutputFile to ) { to.writeString(empNum); to.writeString(empName); to.writeDouble(rate); to.writeDouble(YTDPay); to.newLine(); }; // write Output stream is passed to employee object Streams reference is used to indicate where employee record is to be written.

25 COSC 1P02 Intro. to Computer Science 8.25 Report Generation private void setUpReport ( Date date ) { report.setTitle("Timmy's Coffee Bar",getDateInstance().format(date)); report.addField("empNum","Employee #",10); report.addField("empName","Name",20); report.addField("rate","Pay Rate",getCurrencyInstance(),9); report.addField("hours","Hours",getDecimalInstance(2),6); report.addField("pay","Total Pay",getCurrencyInstance(),11); report.addField("YTDPay","YTD Pay",getCurrencyInstance(),12); }; // setUpReport private void writeDetail ( Employee anEmployee, double hours, double pay ) { report.writeString("empNum",anEmployee.getEmpNum()); report.writeString("empName",anEmployee.getEmpName()); report.writeDouble("rate",anEmployee.getRate()); report.writeDouble("hours",hours); report.writeDouble("pay",pay); report.writeDouble("YTDPay",anEmployee.getYTDPay()); }; // writeDetail private void writeSummary ( double totPay, double totYTDPay ) { report.writeString("empNum","Total"); report.writeDouble("pay",totPay); report.writeDouble("YTDPay",totYTDPay); }; // writeSummary

26 COSC 1P02 Intro. to Computer Science 8.26 The end


Download ppt "COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible."

Similar presentations


Ads by Google