Problem overview
This is the first version of a small project. You have one employee, described by three values: name, productivity and salary. The program must work out the employee's end-of-year bonus and print two lines: the name, then the bonus.
The bonus is 15% of the salary, rounded down to a whole number. With name = "John Doe" and salary = 135000, 15% of 135000 is 20250, so the program prints:
John Doe
Bonus: $20250
The productivity value is in the starter code but this version doesn't use it. It is there because the next version builds on this one.
Approach
There are three small jobs: calculate the bonus, print the name, and print the bonus with its label. The calculation is the only part that needs thought, because the result has to be a whole number.
The obvious way to take 15% of a number is to multiply by 0.15. That produces a Double, and you would then have to convert it back to an Int. Integer arithmetic avoids that: multiply the salary by 15 first, then divide by 100. In Kotlin, dividing one Int by another gives an Int and drops the fraction, so the rounding down happens by itself.
Step by step
- Create
bonus. Declare it withval, because the value never changes after it is calculated. Its value issalary * 15 / 100. Kotlin evaluates*and/from left to right, so the multiplication happens first. - Print the name.
println(name)prints the text and ends the line, so the name sits alone on the first line. - Print the bonus line. Join the label
"Bonus: $"and the number with+. When the left side of+is aString, Kotlin turns theInton the right into text.
The solution in Kotlin
val name = "John Doe"
val productivity = 83
val salary = 135000
val bonus = salary * 15 / 100
println(name)
println("Bonus: $" + bonus)
A few Kotlin details matter here:
- Integer division truncates. With
salary = 135050the exact bonus is 20257.5, andsalary * 15 / 100gives20257. Multiplying by0.15instead gives theDouble20257.5; you would need.toInt()to get a whole number. - Order matters.
salary / 100 * 15divides first, throwing away the remainder of the salary, and can give a smaller bonus. Withsalary = 135050it gives20250, not20257. Multiply first, then divide. - The dollar sign. In a Kotlin string,
$starts a template such as"$bonus". A$followed by a quote, as in"Bonus: $", has nothing to substitute, so it is printed as a plain character. If you want a dollar sign directly before a template, write\$:"Bonus: \$$bonus"printsBonus: $20257forsalary = 135050. String + Intworks;Int + Stringdoes not."Bonus: $" + bonuscompiles, butbonus + "Bonus: $"is a compile error, becauseInt'splusonly accepts numbers.val, notvar. Usevaronly for a variable that changes later.productivitystays unused. That is fine here. Leave the line as it is.
Edge cases and common mistakes
- Overflow. An
Intis 32 bits, and overflow wraps around silently.salary * 15overflows once the salary is above about 143 million. Withsalary = 150000000,salary * 15 / 100prints-20449672. If salaries could be that large, useLongfor the arithmetic:salary.toLong() * 15 / 100gives22500000. The lesson's salary is far below the limit, so plainIntis fine. - Floating-point rounding. Going through
Doubleand truncating is easy to get subtly wrong, and the integer formula needs no conversion. - Wrong output text. The tests compare the text exactly, so
Bonus: $20250needs the capital B, the colon, one space and the dollar sign with no space after it. - Two
printlncalls, not one. Each call ends its line, which gives the two lines the output needs.
Testing
Try a few salaries in the editor above. The first row is the lesson's own data; the others come from running the same code with a different salary:
| salary | printed bonus line |
|---|---|
| 135000 | Bonus: $20250 |
| 135050 | Bonus: $20257 |
