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
- Write one expression for
score. Under the starter variables, declare avalnamedscore. Its value is three terms added together, one for each weight. - 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. - 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, soscoreis anInt. No type annotation is needed. - Order inside a term.
homework * (20 / 100)looks similar but is different:20 / 100is 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. Writingval score: Int = homework * 0.2doesn't compile. The compiler reportstype mismatch: inferred type is 'Double', but 'Int' was expected, because anInttimes aDoubleis aDouble. Kotlin never narrows a value silently.
Edge cases and common mistakes
- Dividing before multiplying.
20 / 100 * homeworkevaluates20 / 100first, 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 asexam, 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, finalExam | Output |
|---|---|
| 90, 82, 88 | Score: 86 |
| 99, 99, 99 | Score: 97 |
| 5, 5, 5 | Score: 4 |
| 100, 100, 100 | Score: 100 |
| 0, 0, 0 | Score: 0 |
