Java Arena
Step 4 of 4

Testing with different inputs

A program that gives the right answer for one input can still be wrong. Bugs like to hide in the inputs nobody tried. So test with a few kinds of input on purpose:

  • A typical case, like the example in the task.
  • The edges: the smallest and largest values that are allowed, 0, 1, an empty input or a single value, and the values right at a limit (if something changes at 18, try 17 and 18).
  • The unusual: negative numbers, all values the same, the largest value first or last.

This program finds the largest number typed. It works for the example 3, 9, 4:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.valueOf(scanner.nextLine());
        int largest = 0;
        for (int i = 0; i < count; i++) {
            int number = Integer.valueOf(scanner.nextLine());
            if (number > largest) {
                largest = number;
            }
        }
        System.out.println("Largest: " + largest);
    }
}
Input
3
3
9
4
Output
Largest: 9

Now try numbers that are all negative, -5, -2, -8: it prints Largest: 0, a number that wasn't even typed. Starting from 0 only works when some number is bigger than 0. The fix is to start from the first number itself, and compare the rest with it.

Tracing by hand

For a short loop, you can also play the computer: write the variables in a table and fill in one row per round, without running anything. With the numbers -5, -2, -8:

round number largest after the round
before the loop 0
1 -5 0
2 -2 0
3 -8 0

The table shows the problem at once: largest never changes, because no number beats 0. Next to each row you can also write what the value should be: the first row where the two differ is where the bug is.

Your task

Challenge 1 of 3

Fix the program above so it works for every input, negative numbers too. It reads how many numbers follow, then the numbers (at least one). With the input 3, -5, -2, -8 it prints:

Largest: -2

Challenge 2

A swimming pool charges by age: under 7 swim free, 7 to 17 pay 3 euros, and 18 or older pay 6 euros. This program gets some ages wrong. Test it with the ages at the edges (6, 7, 17 and 18), then fix it. With the input 17 it prints:

Price: 3 euros

Challenge 3

Trace this program by hand, with a table for a and b, then write down what it prints.