Problem overview
This is the first version of the Report Card project. The starter code holds three test scores in val constants, test1, test2 and test3. The program must work out the student's average as a whole number and print it after the text Average: .
With the scores 85, 92 and 78 the sum is 255, and 255 divided by 3 is exactly 85, so the program prints:
Average: 85
The statement asks for integer division. In Scala, dividing an Int by an Int gives an Int and throws away the remainder, so no rounding code is needed. Later versions of the project build on this one.
Approach
An average is the sum divided by the count. There are three scores, so the program adds them and divides by 3. Both sides of the division are Int, which means the result is the quotient with the fractional part cut off. It truncates toward zero, so for positive scores it rounds down.
The result is stored in a val, because it is computed once and never changes. Printing comes last, as a separate step, so the calculation stays easy to read.
Step by step
- Create
average. Declare avalnamedaveragewith the typeInt, and give it the value of the three tests added together, divided by 3. The sum must be complete before the division happens. - Print the result. Use
printlnwith ansstring. Thesin front of the opening quote turns on interpolation, and$averageinside the text is replaced by the variable's value.
The solution in Scala
val test1: Int = 85
val test2: Int = 92
val test3: Int = 78
val average: Int = (test1 + test2 + test3) / 3
println(s"Average: $average")
Two details are worth knowing.
Parentheses decide what is divided
Division binds tighter than addition, as in school arithmetic. Without the parentheses, test1 + test2 + test3 / 3 divides only test3 and then adds the other two. With the starter values it gives 85 + 92 + 26:
val a = 85; val b = 92; val c = 78
println(a + b + c / 3)
println((a + b + c) / 3)
203
85
The remainder is dropped, not rounded
Integer division never rounds to the nearest value. Scores of 70, 71 and 72 add up to 213 and give 71. Scores of 0, 0 and 2 add up to 2, and 2 divided by 3 is 0, not 1. With scores of 100, 100 and 99 the sum is 299 and the result is 99, even though the exact average is 99.67.
If you divide by 3.0 instead, the result is a Double: (a + b + c) / 3.0 is 85.0 for the starter values. That prints a decimal point, so it doesn't match the expected output Average: 85.
Edge cases and common mistakes
- Missing parentheses. This is the most common slip, and it prints 203 instead of 85.
- Using
varfor no reason. The average is assigned once, sovalis the right choice. - Forgetting the
s. Without it,println("Average: $average")prints the characters$averageas they are. - Int overflow. An
Intis 32 bits and wraps silently. For three test scores this is no concern, but a sum that passes 2,147,483,647 would turn negative. Scores of 2147483647, 2147483647 and 1 give a sum that wraps to -1, so the average prints 0.
Key takeaways
Int / Intin Scala gives anIntand drops the remainder.- Put the sum in parentheses before dividing.
s"...$name..."places a variable's value inside a string.
