Java Arena
Step 7 of 8

Sums, averages and the largest value

A loop over the indexes can combine all the values into one result. For a sum, start from 0 and add each value:

ArrayList<Integer> minutes = new ArrayList<>();
minutes.add(25);
minutes.add(40);
minutes.add(15);
minutes.add(30);

int total = 0;
for (int i = 0; i < minutes.size(); i++) {
    total += minutes.get(i);
}
System.out.println("Total: " + total);
System.out.println("Average: " + (double) total / minutes.size());
Output
Total: 110
Average: 27.5

The average divides the sum by size(), and (double) keeps the decimals. (On an empty list, that divides by 0, and the average comes out as NaN, "not a number". Check the size first when the list may be empty.)

The largest value

To find the largest value, keep the largest one seen so far in a variable, and compare every value with it. Start with the first value in the list, not with 0:

ArrayList<Integer> temperatures = new ArrayList<>();
temperatures.add(-3);
temperatures.add(-8);
temperatures.add(-1);
temperatures.add(-5);

int highest = temperatures.get(0);
for (int i = 1; i < temperatures.size(); i++) {
    if (temperatures.get(i) > highest) {
        highest = temperatures.get(i);
    }
}
System.out.println("Highest: " + highest);
Output
Highest: -1

Starting with 0 would give the wrong answer here: no temperature is above 0, so the program would print Highest: 0, a value that isn't even in the list. The first value is always a real candidate. The loop can then start at index 1, since index 0 has been counted already. (get(0) needs a list with at least one value in it.)

For the smallest value, turn the comparison around, to <. And highest = Math.max(highest, temperatures.get(i)); does the same as the whole if.

Where the largest value is

Sometimes the place matters more than the value: which day was the warmest? Then keep the index of the best value so far, and read the value with get when you need it:

ArrayList<Integer> temperatures = new ArrayList<>();
temperatures.add(-3);
temperatures.add(-8);
temperatures.add(-1);
temperatures.add(-5);

int warmest = 0;
for (int i = 1; i < temperatures.size(); i++) {
    if (temperatures.get(i) > temperatures.get(warmest)) {
        warmest = i;
    }
}
System.out.println("Warmest: day " + (warmest + 1) + ", " + temperatures.get(warmest) + " degrees");
Output
Warmest: day 3, -1 degrees

With >, a tie keeps the earlier index; with >=, the later one would win.

Your task

Challenge 1 of 3

A high jumper's coach wants to know how steady the jumper is. Read the heights of the jumps in centimeters, one per line, until 0, and keep them in a list. Then print the best (highest) jump, and how many jumps came within 5 centimeters of it: at most 5 below the best, the best one included. There's always at least one jump. For the input 175, 182, 150, 178 and 0 the program prints:

Best: 182
Close to best: 2

Here 182 and 178 count, while 175 is 7 centimeters short.

Challenge 2

Fill in the blanks so the program adds up the ratings and prints their average:

Average rating: 3.8

Challenge 3

Read the number of visitors on each day of a festival, one number per line, until -1. Then print the busiest day, counting days from 1, and its number of visitors. When two days tie, the earlier one counts. There's always at least one day. For the input 420, 515, 610, 380 and -1 the program prints:

Busiest: day 3 with 610 visitors