CS 201 OBJECT ORIENTED PROGRAMMING II LECTURE 21

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

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

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

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. 4

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”. 5

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 My. Class implements Serializable 6

The Object. Output. Stream Class The Object. Output. Stream class, coupled with the File. Output. Stream class, provides the functionality to write objects to a file. The Object. Output. Stream class provides a convenient way to write objects to a file. – Its write. Object method takes one argument: the object to be written. 7

Constructors for Writing Objects Class Constructor File. Output. Stream( String filename, boolean mode ) creates a File. Output. Stream 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 File. Not. Found. Exception. Object. Output. Stream( Output. Stream out ) creates an Object. Output. Stream that writes to the Output. Stream out. Throws an 8 IOException.

The write. Object Method Return value Method name and argument list void write. Object( 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. See Examples 11. 16 Flight. Record 2. java and Example 11. 17 Writing. Objects. java 9

Omitting Data from the File The write. Object 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: access. Modifier transient data. Type field. Name Example: private transient double total. Revenue; 10

Reading Objects from a File The Object. Input. Stream class, coupled with File. Input. Stream, provides the functionality to read objects from a file. The read. Object method of the Object. Input. Stream class is designed to read objects from a file. Because the read. Object 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 read. Object method throws an EOFException, so we detect the end of the file when we catch that exception. 11

Constructors for Reading Objects Class Constructor File. Input. Stream( String filename ) constructs a File. Input. Stream object from a String representing the name of a file. Throws a File. Not. Found. Exception. Object. Input. Stream( Input. Stream in ) creates an Object. Input. Stream from the Input. Stream in. Throws an IOException. 12

The read. Object Method Return value Object Method name and argument list read. Object( ) 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 Class. Not. Found. Exception See Example 11. 18 Reading. Objects. java Note: – we detect reaching the end of the file in the EOFException catch block –we use a finally block to close the file 13

Example Of Writing Objects • We could serialize the Vehicle. A class by making a minor change to the class: public class Vehicle. A 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; 14

Example Of Writing Objects • Now in a Vehicle. Client class we can save the state of a particular Vehicle. A object by writing it into a file. • The file name usually has the extension. ser i. e. My. File. ser Thus: 15

Example Of Writing Objects Vehicle. A va=new Vehcile. A(“My Vehicle”, 2, 3, 2. 1, 3. 5); try{ File. Output. Stream fos=new File. Output. Stream(“My. File. ser”); Object. Output. Stream oos=new Object. Output. Stream(fos); oos. write. Object(va); oos. close(); } catch(IOException ioe) { System. out. println(“Error writing into file”); ioe. print. Stack. Trace(); } 16

Example of Reading the Object From the File try{ File. Input. Stream fis = new File. Input. Stream(“My. File. ser”); Object. Input. Stream ois = new Object. Input. Stream(fis); Vehicle. A vh = (Vehicle. A)ois. read. Object(); ois. close(); } Catch(IOException ioe) { System. out. println(“Error writing into file”); ioe. print. Stack. Trace(); } • We can output the object’s attributes and observe that the values are the same as the ones we wrote in the file. 17

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. 18

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. 19

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 20

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” (n – 1) times • This is the general case. – We have reduced the size of the problem from size n to size (n – 1). 21

Printing “Hello World” n Times Using Recursion Printing “Hello World” (n – 1) times will be done by: – 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. 22

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

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

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 multiply. Absolute. Value. By 3( int n ) { return ( 3 * Math. abs( n ) ); } 25

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 ); 26

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 a smaller, similar problem? 27

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. 28

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 Recursive. Factorial. java 29

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 Stack. Overflow. Error. 30

Study Guide • Chapter 11 – Section 11. 7, 11. 8 • Chapter 13 – Section 13. 1, 13. 2 31
- Slides: 31