Department summary statistics
For the executive summary, HR requires aggregate statistics including total payroll, average salary, and the highest salary. You'll need to combine all salary values into single summary figures.
The Tablesaw library has been imported, and employees.csv contains the salary data.
This exercise is part of the course
Importing Data in Java
Exercise instructions
- Aggregate all salaries to calculate the total payroll.
- Compute the average salary using the total and row count.
- Find the highest salary in the dataset.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class DepartmentSummaryStats {
public static void main(String[] args) {
Table employees = Table.read().csv("employees.csv");
DoubleColumn salaryCol = employees.intColumn("Salary").asDoubleColumn();
// Aggregate salaries for total payroll
double totalPayroll = salaryCol.____(0.0, Double::____);
// Compute average salary
double avgSalary = totalPayroll / employees.____();
// Find highest salary
double maxSalary = salaryCol.reduce(0.0, Double::____);
System.out.println("Department Summary Statistics:");
System.out.println("=================================");
System.out.println("Total Employees: " + employees.rowCount());
System.out.println("Total Payroll: $" + totalPayroll);
System.out.println("Average Salary: $" + avgSalary);
System.out.println("Highest Salary: $" + maxSalary);
}
}