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.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
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
Get step-by-step solutions from verified subject matter experts
