Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Implementation of Java Program To Demonstrate File Handling and Object Serialization

Uploaded by

harsita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Implementation of Java Program To Demonstrate File Handling and Object Serialization

Uploaded by

harsita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IMPLEMENTATION OF JAVA PROGRAM TO DEMONSTRATE FILE

HANDLING AND OBJECT SERIALIZATION


PROGRAM CODE:
import java.io.*;
class Salary implements Serializable {
private static final long serialVersionUID = 1L;
double m_salary;
double other;
Salary(double m, double o) {
this.m_salary = m;
this.other = o;
}
double totalsalary() {
return m_salary + other;
}
public String toString() {
return "Salary [Base Salary: " + m_salary + ", Other: " + other + ", Total: " +
totalsalary() + "]";
}
}
class Wages implements Serializable {
private static final long serialVersionUID = 1L;
double home_wages;
double repair_charges;
double medicinal_wages;
Wages(double wages, double charges, double medicine) {
this.home_wages = wages;
this.repair_charges = charges;
this.medicinal_wages = medicine;
}
double totalwages() {
return home_wages + repair_charges + medicinal_wages;
}
public String toString() {
return "Wages [Home: " + home_wages + ", Repair: " + repair_charges + ",
Medical: " + medicinal_wages +
", Total: " + totalwages() + "]";
}
}
class Savings implements Serializable {
private static final long serialVersionUID = 1L;
double tsalary;
double twages;
Savings(double sala, double wage) {
this.tsalary = sala;
this.twages = wage;
}
double savingsEarned() {
return tsalary - twages;
}
public String toString() {
return "Savings [Total Salary: " + tsalary + ", Total Wages: " + twages + ",
Savings: " + savingsEarned() + "]";
}
}
public class Exp8 {
public static void main(String[] args) {
try {
Salary sal = new Salary(32000, 5000);
Wages wag = new Wages(20000, 3000, 5000);
Savings sav = new Savings(sal.totalsalary(), wag.totalwages());
FileOutputStream fileOut = new FileOutputStream("data.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(sal);
out.writeObject(wag);
out.writeObject(sav);
out.close();
fileOut.close();
System.out.println("Data has been serialized to 'data.ser'");
FileInputStream fileIn = new FileInputStream("data.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
Salary deserializedSal = (Salary) in.readObject();
Wages deserializedWag = (Wages) in.readObject();
Savings deserializedSav = (Savings) in.readObject();
in.close();
fileIn.close();
System.out.println("\nDeserialized Data:");
System.out.println(deserializedSal);
System.out.println(deserializedWag);
System.out.println(deserializedSav);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
OUTPUT:
IMPLEMENTATION OF JAVA PROGRAM TO DEMONSTRATE
MANIPULATIONS OF DATA USING COLLECTIONS
PROGRAM CODE:
import java.util.*;
class Salary {
double m_salary;
double other;
Salary(double m, double o) {
this.m_salary = m;
this.other = o;
}
double totalsalary() {
return m_salary + other;
}
public String toString() {
return "Salary [Base: " + m_salary + ", Other: " + other + ", Total: " +
totalsalary() + "]";
}
}
class Wages {
double home_wages;
double repair_charges;
double medicinal_wages;
Wages(double wages, double charges, double medicine) {
this.home_wages = wages;
this.repair_charges = charges;
this.medicinal_wages = medicine;
}
double totalwages() {
return home_wages + repair_charges + medicinal_wages;
}
public String toString() {
return "Wages [Home: " + home_wages + ", Repair: " + repair_charges + ",
Medical: " + medicinal_wages +
", Total: " + totalwages() + "]";
}
}
class Savings {
double tsalary;
double twages;
Savings(double sala, double wage) {
this.tsalary = sala;
this.twages = wage;
}
double savings_earned() {
return tsalary - twages;
}
public String toString() {
return "Savings [Total Salary: " + tsalary + ", Total Wages: " + twages + ",
Savings: " + savings_earned() + "]";
}
}
public class Exp9 {
public static void main(String[] args) {
List<Salary> salaries = new ArrayList<>();
salaries.add(new Salary(32000, 5000));
salaries.add(new Salary(35000, 7000));
List<Wages> wages = new LinkedList<>();
wages.add(new Wages(20000, 3000, 5000));
wages.add(new Wages(15000, 2000, 3000));
Set<Double> uniqueSalaries = new HashSet<>();
for (Salary sal : salaries) {
uniqueSalaries.add(sal.totalsalary());
}
Set<Double> sortedSavings = new TreeSet<>();
for (int i = 0; i < salaries.size() && i < wages.size(); i++) {
double totalSalary = salaries.get(i).totalsalary();
double totalWages = wages.get(i).totalwages();
sortedSavings.add(new Savings(totalSalary, totalWages).savings_earned());
}
Map<Salary, Wages> salaryToWages = new HashMap<>();
for (int i = 0; i < salaries.size() && i < wages.size(); i++) {
salaryToWages.put(salaries.get(i), wages.get(i));
}
System.out.println("ArrayList (Salaries):");
salaries.forEach(System.out::println);
System.out.println("\nLinkedList (Wages):");
wages.forEach(System.out::println);
System.out.println("\nHashSet (Unique Salaries):");
uniqueSalaries.forEach(System.out::println);
System.out.println("\nTreeSet (Sorted Savings):");
sortedSavings.forEach(System.out::println);
System.out.println("\nHashMap (Salary to Wages Mapping):");
salaryToWages.forEach((key, value) ->
System.out.println("Salary: " + key + " -> Wages: " + value));
}
}
OUTPUT:

You might also like