THINK FIRST·CODE LATER

← All labs

Leaderboard with a multi-key comparator

Problem

Read a class's exam results and print a leaderboard.

Input. An integer n (n ≥ 0), then n lines each with a name (one word, no spaces) and an integer score.

Output. If n is 0, print No students. and stop. Otherwise sort the students by score, highest first; students with equal scores are ordered by name in natural (alphabetical) order. Print one line per student: rank. name score. Tied students share a rank, and the next rank skips accordingly (1, 1, 3, …). Finally print Average: followed by the mean score rounded to one decimal place (use Math.round(avg * 10) / 10.0).

Build the ordering with Comparator.comparing, reversed and thenComparing, and compute the average with a stream.

Input:
4
Lina 92
Omar 85
Bea 92
Zed 70

Output:
1. Bea 92
1. Lina 92
3. Omar 85
4. Zed 70
Average: 84.8

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d3-1-data-1.txt

4
Lina 92
Omar 85
Bea 92
Zed 70
What your program must print for d3-1-data-1.txt
1. Bea 92
1. Lina 92
3. Omar 85
4. Zed 70
Average: 84.8

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d3-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content d3-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.