What the exercise asks
This exercise combines several things you have already met: variables, arithmetic, a comparison and an if-else. You start with three variables holding a student's grades:
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. A student passes with an average of 5 or more.
Step 1: compute the average
The average is the sum of the grades divided by how many there are. Declare a new val named grade for it:
val grade = (mathGrade + englishGrade + scienceGrade) / 3.0
Two details matter here.
- The parentheses. Division happens before addition. Without them,
mathGrade + englishGrade + scienceGrade / 3.0divides only the science grade and then adds the other two. With the grades above that gives14.0instead of the real average. - The
3.0. Dividing anIntby anotherIntis integer division in Kotlin: it drops the fractional part. With grades 4, 4 and 5 the sum is 13, and13 / 3is4, while13 / 3.0is4.333333333333333. Dividing by3.0, aDouble, makes the whole result aDouble, so the fraction is kept. Kotlin does not convert silently the other way. Writingval avg: Double = 13 / 3is a compile error, because13 / 3is anInt.
For the starting grades, 7 + 4 + 9 is 20, and 20 divided by 3.0 is 6.666666666666667.
Step 2: check the average
Write an if whose condition is grade >= 5. The >= operator is true when grade is 5 or more, so an average of exactly 5 passes. Comparing a Double with the whole number 5 is fine here: >= works across Double and Int, unlike ==, which would be rejected.
Step 3: the passing branch
Inside the if block, print "Passed the test" with println.
Step 4: the failing branch
Add an else block that prints "Failed the test". It needs no condition, because it runs whenever grade >= 5 is false.
The full solution
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 average is about 6.67, which is at least 5, so the program prints Passed the test. Change englishGrade and scienceGrade to 2 and the average falls below 5, so the output becomes Failed the test.
Trying other grades
Changing the three starting values shows how the boundary behaves:
- Grades 5, 5 and 5 give an average of exactly
5.0, and the program printsPassed the test. - Grades 4, 5 and 5 give
4.666666666666667, which is below 5, so it printsFailed the test. - Grades 10, 1 and 4 also average
5.0. One very high grade can balance a very low one, because the average only looks at the total.
Notice what the program is doing in each case. First it turns three separate values into one number. Then it asks a single yes-or-no question about that number. Finally it picks one of two messages. Most small programs have that shape: compute, decide, report.
Common mistakes
- Forgetting the parentheses. This is the most frequent error, and it produces a plausible-looking number, so nothing warns you.
- Using
>instead of>=. An average of exactly 5 would then fail. Check the boundary. - Using
valwhen the value must change. Here every value is set once, sovalis right. Only switch tovarif you need to reassign. - Rounding by hand. Do not convert the average to an
Intbefore comparing. Truncating 4.67 gives 4 and hides the real value, which matters as soon as a threshold is not a whole number. - Printing the grade by accident. The task asks for a message about passing, not for the number itself.
Key takeaways
- Use parentheses so the sum is computed before the division.
- Divide by a
Doublesuch as3.0to keep the fractional part of an average. - A comparison produces a
Booleanthat decides which branch ofif-elseruns. - Test the boundary value, here an average of exactly 5.