Sunday, February 12, 2012

IMPLEMENTATION OF OBJECT SERIALISATION [ java ]


IMPLEMENTATION OF OBJECT SERIALISATION

 

AIM:
            To write a java program to design classes for currency, rupee and dollar and to generate random rupee and dollar objects and write then into file and also to read that  file  that rupee to dollar.

ALGORITHM:

            SERIALISATION WRITE

                        1. Create the class currency as base class
                        2. Define the classes rupee and dollar from the base class currency.
                        3. Generate a random number ,and create an object and copy the content to                                        serial.txt and if there are any exception throw the exception.
                       
            SERIALISATION READ
                        1. Create the class serial read and using that class generate the random number.
                        2 .Using the class read the file in which dollar is converted to rupee

PROGRAM:

SERIALIZATIONWRITE

import java.io.*;
import java.util.*;
import java.lang.*;

class Currency implements Serializable
{
protected String currency;
protected int amount;

public Currency(String cur, int amt)
{
this.currency = cur;
this.amount = amt;
}
public String toString()
{
return currency + amount;
}
public String dollarToRupee(int amt)
{
return "Rs" + amt * 45;
}
}
class Rupee extends Currency
{
public Rupee(int amt)
{
super("Rs",amt);
}
}
class Dollar extends Currency
{
public Dollar(int amt)
{
super("$",amt);
}
}
class SerializationWrite
{
public static void main(String args[])throws IOException
{
Random r = new Random();
try
{
Currency currency;
FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing to file using Object Serialization:");
for(int i=1;i<=25;i++)
{
Object[] obj = { new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000)) };
currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $
System.out.println(currency);
oos.writeObject(currency);
oos.flush();
}
oos.close();
}
catch(Exception e)
{
System.out.println("Exception during Serialization: " + e);
}
}
}



SERIALIZATIONREAD

import java.io.*;
import java.util.*;
import java.lang.*;

class SerializationRead
{
public static void main(String args[])
{
try
{
Currency obj;
FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Reading from file using Object Serialization:");
for(int i=1;i<=25;i++)
{
obj = (Currency)ois.readObject();
if((obj.currency).equals("$"))
System.out.println(obj + " = " + obj.dollarToRupee(obj.amount));
else
System.out.println(obj + " = " + obj);
}
ois.close();
}
catch(Exception e)
{
System.out.println("Exception during deserlization." + e);
}
}
}













OUTPUT
 S:/>javac SerializationWrite.java
S:?>java SerializationWrite
Writing to file using Object Serialization
$1501
$4181
Rs2603

 S:/>javac SerializationRead.java
S:?>java SerializationRead
Reading to file using Object Serialization
$1501=Rs67545
$4181=Rs188145
Rs2603=Rs2603























RESULT:
            Thus the object serialization for currency conversion using random number generation       was implemented.

No comments:

Post a Comment