Chỉnh sửa cấu trúc bảng
Trạm khí tượng cần cập nhật bảng dữ liệu thời tiết để bổ sung nhiệt độ dự báo. Họ muốn thêm nhiệt độ dự báo nhưng vẫn giữ nhiệt độ ghi nhận thực tế để so sánh.
Các lớp Table, DoubleColumn và StringColumn từ Tablesaw đã được nhập sẵn cho bạn.
Bài tập này là một phần của khóa học
Nhập dữ liệu trong Java
Hướng dẫn bài tập
- Đổi tên cột "Temperature" thành "ActualTemp".
- Thêm một cột mới "ForecastTemp" vào bảng.
- In cấu trúc bảng.
- In 3 hàng đầu tiên.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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.____(____));
}
}