Assume that a file containing employee records is named employees.txt and exists
ID: 3669501 • Letter: A
Question
Assume that a file containing employee records is named employees.txt and exists on your computer's disk. Each employee record occupies two lines; one contains employee's name and the other one contains employee's salary. Write a program to read out all such records stored in employees.txt, and display a table of employee names and salaries. At the bottom of the table, you also have to display the average of employee salaries. A sample file is provided for your unit testing. However, the instructor will put different records in employees.txt for testing your submitted program. User interface specifications: Output The employee table shall have two columns and have appropriate headers. Each column of the table must be aligned. The first column is for employee names and the second column is for employee salaries. The average of employee salaries. Testing: Develop at least 2 test cases, calculate the correct results by hand, and then confirm that your program gives the same results.Explanation / Answer
import java.io.File;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class Employee {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
// reading data from file
scanner = new Scanner(new File("employees.txt"));
double salarySum = 0.0d;
int count = 0;
// printing table
System.out.print("Name Salary ");
while (scanner.hasNext()) {
count++;
System.out.print(scanner.next() + " ");
double salary = scanner.nextDouble();
// summing salaries
salarySum += salary;
System.out.println(salary);
}
// caliculating salary
double averageSalary = salarySum / count;
// printing average salary
System.out.println("Average Salary:" + averageSalary);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
employees.txt
Srinu
60000
Rajesh
58000
Pavan
87552
OUTPUT:
Srinu 60000.0
Rajesh 58000.0
Pavan 87552.0
Average Salary:68517.33
NOTE: it is implemented in java if you want in some other language please specify i
will.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.