Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS"— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
CS 201 OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS Copyright: Illinois Institute of Technology/ George Koutsogiannakis

2 Last Week’s Topics The java.io Package Reading Text Files
Reading Structured Text Files Using StringTokenizer Reading using Streams Writing to Structured Text Files

3 New Topics Writing objects into a File. Reading objects from a File.
Simple Recursion Recursion with a Return Value

4 Serialization Writing objects into a file involves a protocol of Java called: Serialization Objects are converted (serialized) into a byte stream and stored in the file. Reading objects from a file involves a protocol called deserialization. Objects are converted back to the particular data type that they belong to.

5 Serialization A State of an object is the particular value sof its attributes (fields) at a particular time. Saving a particular state via serialization is called “persistence”.

6 Writing Objects to a File
To write an object to a file, its class must implement the Serializable interface, which indicates that: the object can be converted to a byte stream to be written to a file that byte stream can be converted back into a copy of the object when read from the file The Serializable interface has no methods to implement. All we need to do is: import the java.io.Serializable interface add implements Serializable to the class header i.e public class MyClass implements Serializable

7 The ObjectOutputStream Class
The ObjectOutputStream class, coupled with the FileOutputStream class, provides the functionality to write objects to a file. The ObjectOutputStream class provides a convenient way to write objects to a file. Its writeObject method takes one argument: the object to be written.

8 Constructors for Writing Objects
Class Constructor FileOutputStream FileOutputStream( String filename, boolean mode ) creates a FileOutputStream object from a String representing the name of a file. If the file does not exist, it is created. If mode is false, the current contents of the file, if any, will be replaced. If mode is true, writing will append data to the end of the file. Throws a FileNotFoundException. ObjectOutputStream ObjectOutputStream( OutputStream out ) creates an ObjectOutputStream that writes to the OutputStream out. Throws an IOException.

9 The writeObject Method
See Examples FlightRecord2.java and Example WritingObjects.java Return value Method name and argument list void writeObject( Object o ) writes the object argument to a file. That object must be an instance of a class that implements the Serializable interface. Otherwise, a run-time exception will be generated. Throws an IOException.

10 Omitting Data from the File
The writeObject method does not write any object fields declared to be static or transient. You can declare a field as transient if you can easily reproduce its value or if its value is 0. Syntax to declare a field as transient: accessModifier transient dataType fieldName Example: private transient double totalRevenue;

11 Reading Objects from a File
The ObjectInputStream class, coupled with FileInputStream, provides the functionality to read objects from a file. The readObject method of the ObjectInputStream class is designed to read objects from a file. Because the readObject method returns a generic Object, we must type cast the returned object to the appropriate class. When the end of the file is reached, the readObject method throws an EOFException, so we detect the end of the file when we catch that exception.

12 Constructors for Reading Objects
Class Constructor FileInputStream FileInputStream( String filename ) constructs a FileInputStream object from a String representing the name of a file. Throws a FileNotFoundException. ObjectInputStream ObjectInputStream( InputStream in ) creates an ObjectInputStream from the InputStream in. Throws an IOException.

13 The readObject Method See Example 11.18 ReadingObjects.java Note:
Return value Method name and argument list Object readObject( ) reads the next object and returns it. The object must be an instance of a class that implements the Serializable interface. When the end of the file is reached, an EOFException is thrown. Also throws an IOException and ClassNotFoundException See Example ReadingObjects.java Note: we detect reaching the end of the file in the EOFException catch block we use a finally block to close the file

14 Example Of Writing Objects
We could serialize the VehicleA class by making a minor change to the class: public class VehicleA implements Serializable We can exclude certain fields of the class from being serialized (and thus have their values written) by using the keyword transient in front of the declaration of the field. i.e. private transient double init_v; private transient double init_d;

15 Example Of Writing Objects
Now in a VehicleClient class we can save the state of a particular VehicleA object by writing it into a file. The file name usually has the extension .ser i.e. MyFile.ser Thus:

16 Example Of Writing Objects
VehicleA va=new VehcileA(“My Vehicle”, 2, 3, 2.1, 3.5); try{ FileOutputStream fos=new FileOutputStream(“MyFile.ser”); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(va); oos.close(); } catch(IOException ioe) { System.out.println(“Error writing into file”); ioe.printStackTrace();

17 Example of Reading the Object From the File
try{ FileInputStream fis = new FileInputStream(“MyFile.ser”); ObjectInputStream ois = new ObjectInputStream(fis); VehicleA vh = (VehicleA)ois.readObject(); ois.close(); } Catch(IOException ioe) { System.out.println(“Error writing into file”); ioe.printStackTrace(); We can output the object’s attributes and observe that the values are the same as the ones we wrote in the file.

18 Simple Recursion Sometimes large problems can be solved by transforming the large problem into smaller and smaller problems until you reach an easily solved problem. This methodology of successive reduction of problems is called Recursion. That easy-to-solve problem is called the base case. The formula that reduces the size of a problem is called the general case.

19 Recursive Methods A recursive method calls itself, i.e. in the body of the method, there is a call to the method itself. The arguments passed to the recursive call are smaller in value than the original arguments.

20 Simple Recursion When designing a recursive solution for a problem, we need to do the following: define the base case define the rule for the general case

21 Printing “Hello World” n Times Using Recursion
In order to print “Hello World” n times (n is greater than 0), we can do the following: Print “Hello World” Print “Hello World” (n – 1) times This is the general case. We have reduced the size of the problem from size n to size (n – 1).

22 Printing “Hello World” n Times Using Recursion
Printing “Hello World” (n – 1) times will be done by: Printing “Hello World” Printing “Hello World” (n – 2) times … and so on Eventually, we will arrive at printing “Hello World” 0 times: that is easy to solve; we do nothing. That is the base case.

23 Pseudocode for Printing “Hello World” n Times Using Recursion
printHelloWorldNTimes( int n ) { if ( n is greater than 0 ) print “Hello World” printHelloWorldNTimes( n – 1 ) } // else do nothing

24 Coding the Recursive Method
public static void printHelloWorldNTimes( int n ) { if ( n > 0 ) System.out.println( “Hello World” ); printHelloWorldNTimes( n – 1 ); } // if n is 0 or less, do nothing See Example 13.1 RecursiveHelloWorld.java

25 Recursion with a Return Value
In a value-returning method, the return statement can include a call to another value-returning method. For example, public int multiplyAbsoluteValueBy3( int n ) { return ( 3 * Math.abs( n ) ); }

26 Recursion with a Return Value
In a recursive value-returning method, the return statement can include a call to the method itself. The return value of a recursive value-returning method often consists of an expression that includes a call to the method itself: return ( expression including a recursive call to the method );

27 Calculating a Factorial
The factorial of a positive number is defined as factorial( n ) = n! = n * ( n – 1 ) * ( n – 2 ) * … 3 * 2 * 1 By convention, factorial( 0 ) = 0! = 1 (The factorial of a negative number is not defined.) Can we find a relationship between the problem at hand and a smaller, similar problem?

28 Calculating a Factorial
factorial( n ) = n * factorial( n – 1 ) At each step, the size of the problem is reduced by 1: we progress from a problem of size n to a problem of size (n – 1) A call to factorial( n ) will generate a call to factorial( n – 1 ), which in turn will generate a call to factorial( n – 2 ), …. Eventually, a call to factorial( 0 ) will be generated; this is our easy-to-solve problem. We know that factorial( 0 ) = 1. That is the base case.

29 Code for a Recursive Factorial Method
public static int factorial( int n ) { if ( n <= 0 ) // base case return 1; else // general case return ( n * factorial( n – 1 ) ); } See Example 13.2 RecursiveFactorial.java

30 Common Error Trap In coding a recursive method, failure to code the base case will result in a run-time error. If the base case is not coded, when the method is called, the recursive calls keep being made because the base case is never reached. This eventually generates a StackOverflowError.

31 Study Guide Chapter 11 Chapter 13 Section 11.7, 11.8


Download ppt "OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS"

Similar presentations


Ads by Google