Problem overview
Version 2 of the Report Card computes a weighted final score. The starter code gives three marks: homework is 90, midterm is 82 and finalExam is 88. Homework counts for 20 percent of the score, the midterm for 30 percent and the final exam for 50 percent.
Each weighted part is rounded down to a whole number before the parts are added. For the starting data the parts are 18, 24 and 44, which add up to 86, and the program prints:
Score: 86
Approach
A weight of 20 percent means multiplying by 20 and dividing by 100. Scala's Int division truncates, so homework * 20 / 100 is already rounded down: 90 * 20 is 1800, and 1800 / 100 is 18. The midterm gives 82 * 30 = 2460, which divides to 24, and the final exam gives 88 * 50 = 4400, which divides to 44.
The order matters. Multiplication and division have the same precedence and run from left to right, so homework * 20 / 100 multiplies first and then divides. That keeps the precision: dividing first would lose it.
Step by step
- Create
score. Declare avalnamedscore. For each mark, multiply by its weight and divide by 100, then add the three results. Note that the third variable is calledfinalExam, becausefinalis not a usable name for a variable in Scala 3 code like this. - Print it. Use
println(s"Score: $score"). Thesprefix lets$scoreinsert the number into the text.
The solution in Scala
val homework = 90
val midterm = 82
val finalExam = 88
val score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100
println(s"Score: $score")
Why each part is rounded on its own
The rule says to round each part before adding, and the code does exactly that: three divisions, then two additions. Adding the raw products first and dividing once gives a different answer when the parts have remainders. With every mark equal to 99:
val h = 99; val m = 99; val f = 99
println(h * 20 / 100 + m * 30 / 100 + f * 50 / 100)
println((h * 20 + m * 30 + f * 50) / 100)
97
99
The parts are 19, 29 and 49, which add up to 97. The single division gives 99. The lesson's tests follow the per-part rule, so the first form is the one to write.
Edge cases and common mistakes
- Dividing the weight first. Writing
homework * (20 / 100)is wrong, because20 / 100is 0 in integer arithmetic and the whole product becomes 0. With 99 for homework and midterm,h * (20 / 100) + m * (30 / 100)prints 0. - Using decimals.
homework * 0.2is aDouble, and the sum of threeDoublevalues prints as99.0for the all-99 marks. The expected output is a whole number, so keep everything inInt. - Misnaming the variable. The code must use
finalExam, exactly as the starter code spells it. - Weights that don't add up. 20, 30 and 50 sum to 100, so marks of 100 in all three parts give a score of 100. A quick check like this catches a mistyped weight.
Key takeaways
- A percentage weight in integer math is
mark * weight / 100, with the multiplication first. - Each integer division drops its remainder, so rounding happens part by part.
- Never write
20 / 100on its own with integers: it is 0.
