1. You are given a data file (employeeData.txt) that includes the employee’s fir
ID: 3731346 • Letter: 1
Question
1. You are given a data file (employeeData.txt) that includes the employee’s first name, last name, current salary and pay raise rate.
Write a program that reads the data from the data file, calculates the new salary after the raise for each employee, then stores the results in another file (employeeData2.txt).
The program should also calculate the average current salary and the average new salary. Please use the Java language
Input file (employeeData.txt)
Output file (employeeData2.txt)
Steve Gill 89567.50 5
Rita Johnson 56897.0 6
Randy Brown 78234.5 4.5
Seema Arora 76000.0 3.5
Samir Mann 45345.0 5.5
Samantha McCoy 88990.0 4
Steve Gill 89567.50 5.00 94045.88
Rita Johnson 56897.00 6.00 60310.82
Randy Brown 78234.50 4.50 81755.05
Seema Arora 76000.00 3.50 78660.00
Samir Mann 45345.00 5.50 47838.98
Samantha McCoy 88990.00 4.00 92549.60
Current Average Salary: 72505.67
New Average Salary: 75860.05
Input file (employeeData.txt)
Output file (employeeData2.txt)
Steve Gill 89567.50 5
Rita Johnson 56897.0 6
Randy Brown 78234.5 4.5
Seema Arora 76000.0 3.5
Samir Mann 45345.0 5.5
Samantha McCoy 88990.0 4
Steve Gill 89567.50 5.00 94045.88
Rita Johnson 56897.00 6.00 60310.82
Randy Brown 78234.50 4.50 81755.05
Seema Arora 76000.00 3.50 78660.00
Samir Mann 45345.00 5.50 47838.98
Samantha McCoy 88990.00 4.00 92549.60
Current Average Salary: 72505.67
New Average Salary: 75860.05
Explanation / Answer
EmployeeSalaryRaise.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class EmployeeSalaryRaise {
public static void main(String[] args) throws FileNotFoundException {
String inputFile = "D:\employeeData.txt";
String outputFile = "D:\employeeData2.txt";
File input = new File(inputFile);
File output = new File(outputFile);
int lineCount = 0;
if(input.exists()) {
Scanner scan = new Scanner(input);
PrintWriter pw = new PrintWriter(output);
double totalSalary = 0, newTotalSalary = 0;
while(scan.hasNextLine()) {
lineCount++;
String firstName = scan.next();
String lastName = scan.next();
double currentSalary = scan.nextDouble();
totalSalary+=currentSalary;
double raiseRate = scan.nextDouble();
double newSalary = (currentSalary+(currentSalary*raiseRate)/100);
newTotalSalary+=newSalary;
pw.write(firstName+" "+lastName+" "+currentSalary+" "+raiseRate+" "+newSalary+" ");
}
System.out.println("File has been generated");
pw.write("Current Average Salary: "+totalSalary/lineCount+" ");
pw.write("New Average Salary: "+newTotalSalary/lineCount+" ");
scan.close();
pw.flush();
pw.close();
} else {
System.out.println("File does not exist");
}
}
}
Output:
File has been generated
Steve Gill 89567.5 5.0 94045.875
Rita Johnson 56897.0 6.0 60310.82
Randy Brown 78234.5 4.5 81755.0525
Seema Arora 76000.0 3.5 78660.0
Samir Mann 45345.0 5.5 47838.975
Samantha McCoy 88990.0 4.0 92549.6
Current Average Salary: 72505.66666666667
New Average Salary: 75860.05375
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.