What the exercise asks
The starter code stores three grades as integers:
val mathGrade = 7
val englishGrade = 4
val scienceGrade = 9
The task is to compute the average of the three grades and print whether the student passed. It combines arithmetic, a comparison and an if-else. A student passes with an average of 5 or more.
Step by step
- Compute the average. Declare
val gradeand give it the sum of the three grades divided by3.0. - Write the condition. Pass means 5 or more, so the check is
grade >= 5. - Print the pass message inside the
if:println("Passed the test"). - Print the fail message in the
else:println("Failed the test"). Theelseneeds no condition because it covers every average below 5.
The solution in Scala
val mathGrade = 7
val englishGrade = 4
val scienceGrade = 9
val grade = (mathGrade + englishGrade + scienceGrade) / 3.0
if (grade >= 5) {
println("Passed the test")
} else {
println("Failed the test")
}
The sum is 20, the average is about 6.67, and 6.67 is at least 5, so the program prints Passed the test.
Two details that decide the result
Parentheses around the sum
Division binds tighter than addition, just as in arithmetic class. Without the parentheses, mathGrade + englishGrade + scienceGrade / 3.0 divides only the last grade: 7 + 4 + 9 / 3.0 is 14.0, which is not an average at all. The parentheses make the whole sum the left side of the division.
Why 3.0 and not 3
Dividing an Int by another Int gives an Int, and the fractional part is dropped. Dividing an Int by the Double 3.0 gives a Double, so nothing is lost:
println((7 + 4 + 9) / 3)
println((7 + 4 + 9) / 3.0)
6
6.666666666666667
Integer division matters near the pass mark. A sum of 14 has a true average of 4.67, but 14 / 3 gives 4. With a pass mark of 4.5, the integer version would fail a student who should pass. Dividing by 3.0 avoids the whole class of problem, even though the lesson's mark of 5 hides it in most cases.
Types and names
The type of grade is inferred as Double, so there is no need to write it. Because it is a val, it can't be reassigned, which is what you want for a computed result. Comparing a Double with the integer 5 is fine: Scala converts the 5 for the comparison.
Trying other grades
Change the three values to see both branches. These results follow from the same code:
- Grades 7, 4 and 9 average 6.67, so it prints
Passed the test. - Grades 4, 4 and 3 average 3.67, so it prints
Failed the test. - Grades 5, 5 and 5 average exactly 5.0, so it prints
Passed the test. - Grades 10, 1 and 1 average 4.0, so it prints
Failed the test: one high grade does not rescue a low average.
Edge cases and common mistakes
- An average of exactly 5. It passes, because the operator is
>=. Grades of 5, 5 and 5 give 5.0. - Dividing by
3. The fraction is cut off before the comparison. - Missing parentheses. Only the last grade is divided.
- Using
>. An average of exactly 5.0 would fail. - Changing the variable names. The starter code uses
mathGrade,englishGradeandscienceGrade. Use them exactly.
Key takeaways
- Wrap a sum in parentheses before dividing it.
- Divide by a
Doublesuch as3.0to keep the fractional part of an average. - Use
>=when the boundary value should count as a pass. - A computed value can go straight into an
ifcondition by way of a comparison.
