Problem overview
This is the first version of the Report Card project. A student has taken three tests, and the program must print their average as a whole number. The starter code gives you three Int constants:
val test1: Int = 85
val test2: Int = 92
val test3: Int = 78
The three marks add up to 255, and 255 divided by 3 is exactly 85, so the program prints:
Average: 85
The next version of the project builds on this one.
Approach
An average is the sum of the values divided by how many there are. Here that means adding the three tests and dividing by 3. The lesson asks for a whole number, and Kotlin gives you one without extra work: when both sides of / are Int, the result is an Int too, and the fractional part is simply dropped.
Dropping the fraction is truncation, not rounding to the nearest value. A sum of 254 divided by 3 is 84.67 in maths, but 254 / 3 is 84 in Kotlin. For positive marks this is the same as rounding down.
Step by step
- Compute the average. Under the three starter lines, declare a
valnamedaverage. Its value is the sum of the three tests divided by 3. The sum must be finished before the division happens, which is the one detail that needs care, as the next section shows. - Print it. Use
printlnwith the textAverage:followed by the value. Inside a string literal, a$followed by a name inserts that variable's value, so"Average: $average"produces the line the lesson expects.
The solution in Kotlin
val test1: Int = 85
val test2: Int = 92
val test3: Int = 78
val average: Int = (test1 + test2 + test3) / 3
println("Average: $average")
A few details worth knowing:
- The parentheses matter. Division binds tighter than addition, so without them only the last test is divided.
test1 + test2 + test3 / 3evaluates to85 + 92 + 26, which is 203, not 85. valmeans the name is assigned once.averagenever changes after it is computed, sovalis the right choice. The tests arevalas well.- The type is optional. The compiler infers
Intfrom the expression, soval average = (test1 + test2 + test3) / 3means the same thing. The lesson writes the type out to make it visible. - String templates.
$averagereads a simple name. For anything longer, such as an expression, write it in braces:"${test1 + test2}". - The remainder is not rounded. Because the lesson wants a truncated whole number,
Intdivision is exactly what it needs. If you divide by3.0instead, the result is aDouble, and the same marks print as85.0, which would not match the expected line.
Edge cases and common mistakes
- Missing parentheses. This is the most common slip, and it gives 203 for the lesson's data.
- Using a decimal divisor.
/ 3.0gives aDouble, which prints with a decimal point. - Expecting rounding. Tests of 59, 60 and 60 add up to 179, and 179 divided by 3 is 59 with a remainder of 2. The program prints
Average: 59, not 60. - Averages that truncate to zero. Tests of 0, 0 and 2 give
0, because 2 divided by 3 has no whole part. - Forgetting the print. The lesson's tests read what the program prints, so computing
averagewithout callingprintlnshows nothing.
Testing
Changing the three starter values lets you check the logic. These pairs come from running the same formula:
| test1, test2, test3 | Output |
|---|---|
| 85, 92, 78 | Average: 85 |
| 90, 90, 91 | Average: 90 |
| 100, 100, 99 | Average: 99 |
| 59, 60, 60 | Average: 59 |
| 0, 0, 2 | Average: 0 |
