Question: java /* * implement a class, Person, with below 3 fields: * -id * -name * -age * * after the implementation of this class,

java

/*

* implement a class, Person, with below 3 fields:

* -id

* -name

* -age

*

* after the implementation of this class, below sample code should able to be run and get the result as:

* [msi_id_251 Gregory 13, msi_id_810 Peter 33, msi_id_242 Robert 78]

* [null Gregory 13, null Peter 33, null Robert 78]

*

* sample test class:

*/

public static void main(String[]args) {

Person robert = new Person("msi_id_242", "Robert", 78);

Person gregory = new Person("msi_id_251", "Gregory", 13);

Person peter = new Person("msi_id_810", "Peter", 33);

TreeSet ts = new TreeSet();

ts.add(robert);

ts.add(gregory);

ts.add(peter);

outputPersonToFile("resource/test.txe", ts);

System.out.println(ts); // display the first line of the result

displayPersonFromFile("resource/test.txt"); // display the second line of the result

}

private static void outputPersonToFile(String filePath, TreeSet ts) {

try(FileOutputStream fos = new FileOutputStream(filePath); ObjectOutputStream oos = new ObjectOutputStream(fos)){

oos.writeObject(ts);

}catch(Exception e) {

printStackTrace();

}

}

private static void displayPersonFromFile(String filePath) {

try(FileInputStream fis = new FileInputStream(filePath); ObjectInputStream ois = new ObjectInputStream(fis)){

TreeSet ts = (TreeSet) ois.readObject();

System.out.println(ts);

}catch(Exception e) {

printStackTrace();

}

}

// PLEASE COMPLETE THIS CLASS

class Person{

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!