Answer:
public static void classSplit(Scanner file){
int studentsOf21 = 0, studentsOf22 = 0,studentsOf23 = 0, studentsOf24 = 0;
while (file.hasNext() == true){
String studentName = file.next();
int year = file.nextInt();
switch (year){
case 2021:
studentsOf21++;
break;
case 2022:
studentsOf22++;
break;
case 2023:
studentsOf23++;
break;
case 2024:
studentsOf24++;
break;
}
}
int totalGraduate = studentsOf21+studentsOf22+studentsOf23+studentsOf24;
System.out.printf("students in class of 2021: %.2f%n", students2021*100.0/totalGraduate);
System.out.printf("students in class of 2022: %.2f%n", students2022*100.0/totalGraduate);
System.out.printf("students in class of 2023: %.2f%n", students2023*100.0/totalGraduate);
System.out.printf("students in class of 2024: %.2f%n", students2024*100.0/totalGraduate);
}
Explanation:
The classSplit method of the java class accepts a scanner object, split the object by iterating over it to get the text (for names of students) and integer numbers (for graduation year). It does not need a return statement to ask it prints out the percentage of graduates from 2021 to 2024.