Question & Answer: Create a boat class with at least two properties. Write methods to archive the objects of boa…..

Create a boat class with at least two properties. Write methods to archive the objects of boat type to disk and then reads and print the attributes.

Expert Answer

 

Boat.java

import java.util.*;
import java.io.*;
public class Boat implements Serializable{
String name;
String color;
Boat(String name,String color){
setname(name);
setcolor(color);
}
private void setcolor(String color) {
// TODO Auto-generated method stub
this.color=color;
}
private void setname(String name) {
// TODO Auto-generated method stub
this.name=name;
}
public String getname(){
return this.name;
}
public String getcolor(){
return this.color;
}
}

Boat_main.java
import java.util.*;
import java.io.*;
public class Boat_main {
static String filename;
public static void main(String[] args) {
// TODO Auto-generated method stub
Read_obj();
Write_obj();
}
public static void Read_obj(){
Boat boat=new Boat(“superboat”,”black”);
filename=”src/Assignment/objfile”;
try{
FileOutputStream fs=new FileOutputStream(filename);
ObjectOutputStream objstream=new ObjectOutputStream(fs);
objstream.writeObject(boat);
objstream.close();
fs.close();
}catch(Exception e){
System.out.println(“eror in serialization of object:”+e);
}
}
public static void Write_obj(){
try{
FileInputStream fo=new FileInputStream(filename);
ObjectInputStream objout=new ObjectInputStream(fo);
Boat boatdec=(Boat)objout.readObject();
System.out.println(“boat name: “+boatdec.getname()+” boat color :”+boatdec.color);
}catch(Exception e){
System.out.println(“eror in deserialization :”+e);
}
}
}

output:

Question & Answer: Create a boat class with at least two properties. Write methods to archive the objects of boa..... 1input file:objfile.txt

Question & Answer: Create a boat class with at least two properties. Write methods to archive the objects of boa..... 2this is text file after object is stored in it
and check path of file if you execute in eclipse ide

Still stressed from student homework?
Get quality assistance from academic writers!