Start learning free

LESSON 175 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 - Class Statistics in Java

Practice

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

List<Integer> scores = Arrays.asList(92, 78, 85, 67, 74, 88, 91, 58);

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (integer division).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (System.out.println("Highest: " + highest);).

Use Collections.max / Collections.min on a List<Integer>; sum with a loop.

List<Integer> scores = Arrays.asList(92, 78, 85, 67, 74, 88, 91, 58);

int highest = Collections.max(scores);
int lowest = Collections.min(scores);
int total = 0;
for (int s : scores) total += s;
int average = total / scores.size();

System.out.println("Highest: " + highest);
System.out.println("Lowest: " + lowest);
System.out.println("Average: " + average);

STEP 1 OF 8
    Ctrl + Enter to check

    Report Card V6 in Java

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free