Start learning free

LESSON 202 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in Kotlin: Weighted Score with Int Division

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

val homework = 90
val midterm = 82
val finalExam = 88

Build:

Score: 86

Rules:

  • Compute score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100.
  • Each part is rounded down because Int divided by Int drops the remainder (the third variable is named finalExam).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score, for example println("Score: $score")

The third variable is named finalExam. Int / Int truncates, so each weight already rounds down.

val homework = 90
val midterm = 82
val finalExam = 88

val score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
println("Score: $score")

STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in Kotlin, explained

    Problem overview

    Version 1 averaged three tests with equal importance. Here the three marks count differently: homework is 20% of the final score, the midterm 30% and the final exam 50%. Each weighted part is rounded down to a whole number before the parts are added. The starter code is:

    val homework = 90
    val midterm = 82
    val finalExam = 88
    

    The parts are 90 * 20 / 100 = 18, 82 * 30 / 100 = 24 and 88 * 50 / 100 = 44, and the three parts add up to 86:

    Score: 86
    

    Note that the third variable is called finalExam, not final.

    Approach

    A percentage of a mark is the mark times the weight, divided by 100. Doing it with integers has one rule that decides the whole lesson: multiply first, divide last. homework * 20 / 100 is read left to right, as (homework * 20) / 100. The product 1800 is a precise whole number, and the division by 100 then drops what is left over.

    Each part is rounded down on its own, which is exactly what the lesson asks for. That can differ from rounding once at the end. With marks of 9, 9 and 9, the three parts are 1, 2 and 4, so the sum is 7. Computing (9 * 20 + 9 * 30 + 9 * 50) / 100 in one go gives 9. For the lesson's data both ways give 86, but the lesson's rule is part by part.

    Step by step

    1. Write one expression for score. Under the starter variables, declare a val named score. Its value is three terms added together, one for each weight.
    2. Build each term the same way. A term is the mark, then * and the weight as a percent number, then / 100. The weights 20, 30 and 50 add up to 100, so the three terms together cover the whole score.
    3. Print the result. println("Score: $score") puts the value after the label.

    The solution in Kotlin

            val homework = 90
            val midterm = 82
            val finalExam = 88
    
            val score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
            println("Score: $score")
    

    Details worth knowing:

    • Operator precedence. * and / have the same precedence and run left to right, and both run before +. That is why no parentheses are needed: each term finishes before the additions.
    • Inferred type. All the operands are Int, so score is an Int. No type annotation is needed.
    • Order inside a term. homework * (20 / 100) looks similar but is different: 20 / 100 is 0 in integer division, so the whole term is 0. For homework of 90, that expression gives 0 instead of 18.
    • Decimals don't fit an Int. Writing val score: Int = homework * 0.2 doesn't compile. The compiler reports type mismatch: inferred type is 'Double', but 'Int' was expected, because an Int times a Double is a Double. Kotlin never narrows a value silently.

    Edge cases and common mistakes

    • Dividing before multiplying. 20 / 100 * homework evaluates 20 / 100 first, which is 0, so the term disappears.
    • Rounding loss. Marks of 99, 99 and 99 give 19 + 29 + 49 = 97, even though 100% of 99 is 99. This is the intended behaviour here, since every part is rounded down. Marks of 5, 5 and 5 give 1 + 1 + 2 = 4.
    • The extremes. Marks of 100, 100 and 100 give 100, and marks of 0, 0 and 0 give 0, so the score always stays between 0 and 100 when the marks do.
    • A wrong variable name. The lesson's third variable is finalExam. Using another name, such as exam, gives an unresolved reference error.
    • No print. The lesson's tests check the printed line, so the value has to go through println.

    Testing

    homework, midterm, finalExamOutput
    90, 82, 88Score: 86
    99, 99, 99Score: 97
    5, 5, 5Score: 4
    100, 100, 100Score: 100
    0, 0, 0Score: 0
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free