Student Records
Completed
Read students id name mark1 mark2 mark3. Print a report sorted by average descending, then id: id name average grade (A ≥ 85, B ≥ 70, C ≥ 50, else F), then the class average and the number of students per grade in the order A B C F.
Input
n, then n lines.
Output
n report lines, then two summary lines.
Example
Input
4 3 cy 90 80 100 1 ann 70 75 65 2 bob 40 55 50 4 dan 90 90 90
Output
3 cy 90.0 A 4 dan 90.0 A 1 ann 70.0 B 2 bob 48.3 F class average 74.6 A=2 B=1 C=0 F=1
Editor
Java
Hints
Pre-fill a LinkedHashMap with A B C F so the summary keeps that order.
Solution explanation
Classes, collections, comparators and formatting together make a complete small report generator.
Show solution
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Student {
final int id;
final String name;
final double average;
Student(int id, String name, double average) {
this.id = id;
this.name = name;
this.average = average;
}
String grade() {
return average >= 85 ? "A" : average >= 70 ? "B" : average >= 50 ? "C" : "F";
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
int id = sc.nextInt();
String name = sc.next();
double avg = (sc.nextInt() + sc.nextInt() + sc.nextInt()) / 3.0;
students.add(new Student(id, name, avg));
}
students.sort(Comparator.comparingDouble((Student s) -> -s.average).thenComparingInt(s -> s.id));
Map<String, Integer> perGrade = new LinkedHashMap<>();
for (String g : new String[]{"A", "B", "C", "F"}) perGrade.put(g, 0);
double total = 0;
for (Student s : students) {
System.out.printf("%d %s %.1f %s%n", s.id, s.name, s.average, s.grade());
perGrade.merge(s.grade(), 1, Integer::sum);
total += s.average;
}
System.out.printf("class average %.1f%n", total / n);
StringBuilder sb = new StringBuilder();
perGrade.forEach((g, c) -> sb.append(sb.length() > 0 ? " " : "").append(g).append('=').append(c));
System.out.println(sb);
}
}