Question: Question 1: public class Worker implements Serializable { private int id; private int birthYear; private int salary; //3 private fields public int getBitrhYear ( )
Question 1:
public class Worker implements Serializable { private int id; private int birthYear; private int salary; //3 private fields
public int getBitrhYear ( ) { return birthYear; } public int getSalary( ) { return salary; } public void setSalary (int s) { salary = s; } }
a- Write a void method that takes a string s1 representing a serial file name of Worker objects and another string s2. The method produces a new serial file (of name as the parameter s2) of all workers in input file such that the salary of workers born before 1992 is increased by 150 (other workers salary has no change).
public static void changeSalary (String w)
b- Write a program that reads N worker objects (N is user-input) into file: w1.ser and then call the above method changeSalaryto produce a new file: w2.ser. The program prints both files on screen (using two calls to a method printFile) //-------------------------------------
Question 2:
Resolve Question 1 using text files.
(in java programing language)
//--------------------------------------
the code is there --> but I don't want it with random worker numbers I want it as (n worker object) n is input from user And help me with question 2 from it ------- import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Worker implements Serializable {
private int id;
private int birthYear;
private int salary;
public Worker(int id, int birthYear, int salary) {
this.id = id;
this.birthYear = birthYear;
this.salary = salary;
}
public int getBirthYear() {
return birthYear;
}
public int getSalary() {
return salary;
}
public void setSalary(int s) {
salary = s;
}
@Override
public String toString() {
return "Worker [id=" + id + ", birthYear=" + birthYear + ", salary="
+ salary + "]";
}
}
public class WorkerReadWrite {
public static void printWorkers(ArrayList
for(Worker w: workers) {
System.out.println(w);
}
}
public static ArrayList
ObjectInputStream objectinputstream = null;
ArrayList
try {
FileInputStream streamIn = new FileInputStream(inputFile);
objectinputstream = new ObjectInputStream(streamIn);
listOfWorkers = (ArrayList
for(Worker w: listOfWorkers) {
if(w.getBirthYear() <= 1992) {
w.setSalary(w.getSalary() + 150);
}
}
printWorkers(listOfWorkers);
// write to output file
FileOutputStream fout = new FileOutputStream(outputFile);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(listOfWorkers);
oos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(objectinputstream != null){
objectinputstream .close();
}
}
return listOfWorkers;
}
public static ArrayList
FileOutputStream fout = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fout);
ArrayList
for(int i=0; i listOfWorkers.add(new Worker((int)(Math.random()*1000), 1987 + (int)(Math.random()*15), 200 + (int)(Math.random()*200))); } oos.writeObject(listOfWorkers); oos.close(); return listOfWorkers; } public static void main(String[] args) throws IOException { System.out.println("Writing workers to file w2.ser"); String input = "w2.ser"; String output = "w1.ser"; ArrayList printWorkers(workers); workers = changeSalary(input, output); System.out.println(" After changing the salary:"); printWorkers(workers); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
