Start learning free

LESSON 201 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in Scala: Integer Average of Three Scores

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

val test1: Int = 85
val test2: Int = 92
val test3: Int = 78

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using integer division (Int divided by Int already drops the remainder).
  • Print "Average: " followed by the average, for example println(s"Average: $average")

Scala's Int / Int truncates, so (test1 + test2 + test3) / 3 already gives an integer.

val test1: Int = 85
val test2: Int = 92
val test3: Int = 78

val average: Int = (test1 + test2 + test3) / 3
println(s"Average: $average")

STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in Scala, explained

    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

    1. Create average. Declare a val named average with the type Int, and give it the value of the three tests added together, divided by 3. The sum must be complete before the division happens.
    2. Print the result. Use println with an s string. The s in front of the opening quote turns on interpolation, and $average inside 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 var for no reason. The average is assigned once, so val is the right choice.
    • Forgetting the s. Without it, println("Average: $average") prints the characters $average as they are.
    • Int overflow. An Int is 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 / Int in Scala gives an Int and drops the remainder.
    • Put the sum in parentheses before dividing.
    • s"...$name..." places a variable's value inside a string.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free