การค้นหาข้อมูลในตาราง
คอร์สเปิดตัวแล้วและมีนักเรียนลงทะเบียนเรียบร้อย ภารกิจสุดท้ายคือการค้นหานักเรียนที่มีผลการเรียนดีเด่นและเพิ่งลงทะเบียนไม่นาน เพื่อนำไปใช้ในแคมเปญถัดไป คราวนี้จะทำงานกับชุดข้อมูลใหม่ชื่อ students.csv ซึ่งเก็บข้อมูลของนักเรียนทุกคนพร้อมเกรด โดยคอลัมน์ GPA คือเกรดของนักเรียน และคอลัมน์ EnrollmentDate คือวันที่ลงทะเบียนเรียน
คลาส Table, Selection และ LocalDate ได้นำเข้ามาให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การนำเข้าข้อมูลใน Java
คำแนะนำการฝึกหัด
- สร้าง
Selectionสำหรับ"GPA"ที่มากกว่า3.5 - สร้าง
Selectionสำหรับวันลงทะเบียนหลังจาก1 ม.ค. 2022 - รวม
Selectionทั้งสองเข้าด้วยกัน - กรองตารางข้อมูลนักเรียน
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
public class StudentAnalysis {
public static void main(String[] args) {
try {
Table students = Table.read().csv("students.csv");
// Create a Selection for GPA greater than 3.5
Selection highGpa = students.doubleColumn("____").____(3.5);
// Create a Selection for enrollment after Jan 1, 2022
Selection freshmen = students.dateColumn("EnrollmentDate")
.____(LocalDate.of(2022, 1, 1));
// Combine both Selections
Selection highPerformingFreshmen = highGpa.____(freshmen);
// Filter the students table
Table featured = students.____(highPerformingFreshmen);
System.out.println("High-performing freshmen:");
System.out.println(featured.print());
} catch (Exception e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
}
}