BaşlayınÜcretsiz başlayın

Total compensation calculator

The company needs total compensation figures that combine salary and bonus for each employee. Since this calculation requires values from two different columns in the same row, you'll need to iterate through rows rather than transform a single column.

The Tablesaw library has been imported, and employees.csv contains the Salary and Bonus columns.

Bu egzersiz, kursun bir parçasıdır

Importing Data in Java

Kursa Göz Atın

Egzersiz talimatları

  • Iterate through each row to access both salary and bonus values.
  • Calculate total compensation by summing the two values.
  • Add the totalComp column to the employees table.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

public class TotalCompensation {
    public static void main(String[] args) {
        Table employees = Table.read().csv("employees.csv");
        
        // Iterate through each row
        DoubleColumn totalComp = DoubleColumn.create("TotalCompensation");
        employees.____(row -> {
            double salary = row.getInt("Salary");
            double bonus = row.getInt("Bonus");
            // Sum salary and bonus
            totalComp.append(____ + ____);
        });
        
        // Add totalComp column
        employees.addColumns(____);
        
        System.out.println("Total Compensation Analysis:");
        System.out.println(employees.first(5));
    }
}
Kodu Düzenle ve Çalıştır