Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in java programming language Question(30 points): class Worker implements Serial

ID: 3713124 • Letter: I

Question

in java programming language

Question(30 points): class Worker implements Serializable private int id; private int birth Year; private int salary; //3 private fields public Worker(int id, int birth Year, int salary) f this.id id; this.birth Year birth Year; this.salary salary; public int getID ) return id; ) public int getBitrh Year ) ( return birth Year; public int getSalary( return salary;) public void setSalary (int s) salary s;) A) Write a method that takes a string s1 representing a serial file name of Worker objects. The method should return number of object stored inside file s1. public static int numOfObject (String s1)

Explanation / Answer

public static int numOfObject(String s1) throws IOException, ClassNotFoundException {
FileInputStream fin = new FileInputStream(s1);
ObjectInputStream ois = new ObjectInputStream(fin);
int count = 0;
try {
while (true) {
ois.readObject();
}
} catch (OptionalDataException e) {
if (!e.eof) throw e;
} finally {
ois.close();
}

return count;
}