THINK FIRST·CODE LATER

← All labs

The two best scores, without an array

Problem

TwoHighest reads the number of students n (at least 2), then n lines name score (scores from 0 to 100). It prints the student with the highest score, the student with the second-highest score, the lowest score and the average.

Keep only a few variables that you update inside the loop — no array, no Math.max: the point of the lab is the loop with if. If two students have the same score, the one read first stays ahead. For the input

5
Amina 78
Bo 91
Chen 85
Diallo 91
Eva 60

the program prints

Highest: Bo (91)
Second: Diallo (91)
Lowest score: 60
Average: 81.00

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.

l7-7-data-1.txt

5
Amina 78
Bo 91
Chen 85
Diallo 91
Eva 60
What your program must print for l7-7-data-1.txt
Highest: Bo (91)
Second: Diallo (91)
Lowest score: 60
Average: 81.00

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 < l7-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l7-7-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.