134k views
3 votes
Write a method called classSplit that accepts a file scanner as input. The data in the file represents a series of names and graduation years. You can assume that the graduation years are from 2021 to 2024.Sample input file:Hannah 2021 Cara 2022 Sarah2022Nate 2023 Bob 2022 Josef 2021 Emma 2022Your method should count the number of names per graduation year and print out a report like this --- watch the rounding!:students in class of 2021: 28.57%students in class of 2022: 57.14%students in class of 2023: 14.29%students in class of 2024: 00.00%You must match the output exactly.You may not use any arrays, arrayLists, or other datastructures in solving this problem. You may not use contains(), startsWith(), endsWith(), split() or related functions. Token-based processing, please.There is no return value.

User Amitlicht
by
5.0k points

1 Answer

2 votes

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);

}

Step-by-step 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.

User Mittelmania
by
4.6k points