Please Use Java to answer the following quesiton: For each of the questions you
ID: 3801817 • Letter: P
Question
Please Use Java to answer the following quesiton:
For each of the questions you need to write your approach. Else 50% points will be deducted.
For each of the following, you need to write the program and run the program.
The best way would be to run the program and copy the code in the pdf.
Insert the screen-shot of the output in the pdf file, in which you are writing the program. This carries major points. (30% of the points for each question)
For the question 1,2,4,5 you also need to provide the input files on which you have tested.
5. Write a Java program to merge the content of two files namely “Employee.txt” and “Salary.txt” into “EmployeeDetails.txt”.
Sample contents of Employee-tt Employee Name EmployeeNumber Department Age 30 222 32 333 40 Sample contents of Employee.txt Employee Number Gross Salary Net Salary 10000 12222 20000 22222 222 30000 333 32222 Sample contents of the merged file EmployeeDetails.txt Employee Name EmployeeNumber Department Age GrossSalary Net Salary 10000 AAA 30 12222 BBB 32 22222 222 20000 CCC 40 2222 30000 333Explanation / Answer
//======================== EmployeeDetail.java ===============================//
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeDetail {
public static void main(String[] args) {
String empfile = "Employee.txt";
String salfile = "Salary.txt";
ArrayList<Employee> list = new ArrayList<Employee>();
Scanner sc = null;
try{
sc = new Scanner(new java.io.File(empfile));
while(sc.hasNext()){
Employee e = new Employee();
e.setName(sc.next());
e.setId(sc.nextInt());
e.setDepartment(sc.next());
e.setAge(sc.nextInt());
list.add(e);
}
sc.close();
sc = new Scanner(new java.io.File(salfile));
while(sc.hasNext()){
int id = sc.nextInt();
double gSal = sc.nextDouble();
float nSal = sc.nextFloat();
boolean isfound=false;
for(int i=0;i<list.size();i++){
Employee e = list.get(i);
if(e.getId()==id){
isfound = true;
e.setSalary(nSal);
e.setGrossSalary(gSal);
}
}
if(!isfound){
Employee e = new Employee();
e.setId(id);
e.setSalary(nSal);
e.setGrossSalary(gSal);
list.add(e);
}
}
sc.close();
}catch(FileNotFoundException e){
}
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("EmployeeDetail.txt");
bw = new BufferedWriter(fw);
System.out.println("Emp Name Emp ID Dept Gross Sal Net Sal ");
for(int i = 0; i<list.size();i++){
Employee e = list.get(i);
System.out.println(""+e.getName()+" "+e.getId()+" "+e.getDepartment()+" "+e.getGrossSalary()+" "+e.getSalary()+" ");
bw.write(""+e.getName()+" "+e.getId()+" "+e.getDepartment()+" "+e.getGrossSalary()+" "+e.getSalary()+" ");
}
bw.close();
System.out.println(" Employee Details are also wiritten in EmployeeDetail.txt file ");
} catch (IOException e) {
e.printStackTrace();
}
}
}
//======================== Employee.java ===============================//
public class Employee {
private String name;
private int id;
private int age;
private float salary;
private String title;
private String department;
private double grossSalary;
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getGrossSalary() {
return grossSalary;
}
public void setGrossSalary(double grossSalary) {
this.grossSalary = grossSalary;
}
}
//================ OUTPUT ==========================//
Emp Name Emp ID Dept Gross Sal Net Sal
Sam 123 Main 4222.0 4000.0
XXX 111 AAA 12222.0 10000.0
YYY 222 BBB 22222.0 20000.0
ZZZ 333 CCC 32222.0 30000.0
Employee Details are also wiritten in EmployeeDetail.txt file
//=====================================================//
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.