Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use.

Similar presentations


Presentation on theme: "COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use."— Presentation transcript:

1 COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use being anything else.” Winston Churchill

2 COSC 1P02 Data Structures and Abstraction 2.2 Files  collections of data stored on external media  disk, tape, CD-ROM, DVD  file management (O/S)  external file name  processing  open  internal file name  file reference  process  close  stream I/O vs record I/O

3 COSC 1P02 Data Structures and Abstraction 2.3 Record I/O  field  indivisible  record  database  key  natural key  assigned key  I/O of entire record from /to binary file  record/structure variables  object-oriented I/O

4 COSC 1P02 Data Structures and Abstraction 2.4 Record Output  object I/O to binary streams only  output  constructor opens file  external file name  writeObject()  subtype  upcasting to Object  always valid  class must implement Serializable  security  interfaces

5 COSC 1P02 Data Structures and Abstraction 2.5 E.g. Create an Employee File  Employee records  Employee class from Example 7.2  Serializable  Class serial identification number  SUID – explicitly stated.  If not explicitly state:  Created at compile time as a computed hash of various class elements: class name, implemented interfaces, declared nonstatic, nontransient fields, declared nonprivate methods, and so on.  data from a text file  Read text file until EOF  reads text record using Employee constructor  writes Employee object using writeObject

6 COSC 1P02 Data Structures and Abstraction 2.6 Record Input  readObject()  must downcast to expected type  not a conversion  ClassCastException  Matches serial number (SUID) of object to object’s class specification  Security  Class Versioning.

7 COSC 1P02 Data Structures and Abstraction 2.7 E.g. List Employee File  not a text file so cannot view in text editor  number of pieces of work  report generation to EOF  no use of Employee constructor  Employee’s are not new  persistent objects  serialization  same class file  can recompile Employee class  If explicit serialVersionUID is used  PayRoll with persistent objects

8 111112.5010000.002300.00 22225.504400.000.00 33337.503000.000.00 444445.0036000.008280.00 PayRoll Employee file.

9

10 COSC 1P02 Data Structures and Abstraction 2.10 Employee Class package Example_7_2; import java.io.*; import BasicIO.*; public class Employee implements Serializable { private static final long serialVersionUID = 123456L; private String empNum; // employee number private double rate; // pay rate private double ytdGross; // year-to-date gross pay private double ytdTax; // year-to-date taxes paid /** The constructor creates a new employee reading the employee * data from a file. * * @param from data file for employee data. */ public Employee ( ASCIIDataFile from ) { empNum = from.readString(); if ( ! from.isEOF() ) { rate = from.readDouble(); ytdGross = from.readDouble(); ytdTax = from.readDouble(); }; }; // constructor This is a java interface,(explained in 1p03) which allows object to be written sequentially to a file. Each class gets a unique identifier so objects can be associated and verified to belong to a specific class. This number can be modified by the programmer if they wish to force objects to comply to a new class specification. Final is the Java equivalent to declaring a constant, i.e. the identifier can not be modified within the program. Signifies that the variable belongs to the class and not the object. All objects created from this class will have the exact same serial number.

11 COSC 1P02 Data Structures and Abstraction 2.11 EmployeeCreate package Example_7_2; import BasicIO.*; public class EmpCreate { private ASCIIDataFile in; // employee data file private BinaryOutputFile out; // new employee data file public EmpCreate () { Employee aE; in = new ASCIIDataFile(); out = new BinaryOutputFile(); while (true){ aE = new Employee(in); if (in.isEOF()) break; out.writeObject(aE); } in.close(); out.close(); }; // construtor public static void main ( String[] args ) { new EmpCreate(); }; } Plain text data file is used for the input BinaryOutputFile supports the binary Object Create an employee object using the constructor of the Employee Class The object is written directly to the Binary data file.

12 COSC 1P02 Data Structures and Abstraction 2.12 Employee List public class EmployeeList { private BinaryDataFile empFile; // employee data file private ASCIIDisplayer out; public EmployeeList ( ) { empFile = new BinaryDataFile(); out = new ASCIIDisplayer(); }; // construtor public void run ( ) { Employee anEmployee; // current employee while (true){ anEmployee = (Employee)empFile.readObject(); if (empFile.isEOF()) break; this.writeDetail(anEmployee); } empFile.close(); out.close(); }; // run private void writeDetail(Employee aE){ out.writeString(aE.getEmpNum()); out.writeString(" "); out.writeDouble(aE.getRate()); out.writeString(" "); out.writeDouble(aE.getYtdGross()); out.writeString(" "); out.writeDouble(aE.getYtdTax()); out.writeString(" "); out.writeEOL(); } Open the binary data file Read an Object from the input file. The Object is then down casted to an Employee object, the serial number of the read object is matched to the Employee class file for validation. Send the object to the writeDetail method which writes its contents to the ASCIIDisplayer.

13 COSC 1P02 Data Structures and Abstraction 2.13 PayRoll private void runPayroll ( ) { int numEmp; // number of employees Employee anEmployee; // current employee double hours; // hours worked double pay; // weekly pay int button; // button pressed numEmp = 0; while ( true ) { anEmployee = (Employee)empFile.readObject(); if ( empFile.isEOF() || !empFile.successful()) break; payForm.clearAll(); fillForm(anEmployee); button = payForm.accept("Pay","Next"); if ( button == 0 ) { hours = payForm.readDouble("hours"); pay = anEmployee.calculatePay(hours); fillForm(anEmployee); payForm.writeDouble("pay",pay); payForm.accept(); writeDetail(anEmployee,hours,pay); }; newEmpFile.writeObject(anEmployee); numEmp = numEmp + 1; }; }; // runPayroll Only modification, instead of creating objects from the Employee constructor, they are read from a BinaryDataFile which contains Employee Objects

14 COSC 1P02 Data Structures and Abstraction 2.14 The End


Download ppt "COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use."

Similar presentations


Ads by Google