CommencerCommencer gratuitement

Modifying table structure

The meteorological station now needs to update its weather data table to accommodate new temperature forecasts. They want to add forecast temperatures while keeping the actual recorded temperatures for comparison purposes.

The Table, DoubleColumn, and StringColumn classes from Tablesaw have been imported for you.

Cet exercice fait partie du cours

Importing Data in Java

Afficher le cours

Instructions

  • Rename the "Temperature" column to "ActualTemp".
  • Add a new "ForecastTemp" column to the table.
  • Print the table structure.
  • Print the first three rows.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

public class ModifyWeatherTable {
    public static void main(String[] args) {
        StringColumn days = StringColumn.create("Day", 
            "Monday", "Tuesday", "Wednesday");
            
        DoubleColumn temperatures = DoubleColumn.create("Temperature", 
            22.5, 24.0, 23.2);
            
        DoubleColumn precipitation = DoubleColumn.create("Precipitation", 
            0.0, 2.5, 5.2);
            
        Table weatherData = Table.create("WeatherData")
            .addColumns(days, temperatures, precipitation);
        
        // Rename the Temperature column
        weatherData.____("Temperature").____("____");
        
        // Add a new ForecastTemp column
        DoubleColumn forecastTemps = DoubleColumn.create("ForecastTemp", 
            23.0, 25.2, 22.8);
        weatherData = weatherData.____(forecastTemps);
        
        // Print the table structure
        System.out.println(weatherData.____());
        
        // Print the first three rows
        System.out.println(weatherData.____(____));
        
    }
}
Modifier et exécuter le code