Problem overview
This is the first version of a small project. The program holds the data of one employee in three values: name, productivity and salary. It must print the name, then the end-of-year bonus, which is 15% of the salary rounded down to a whole number.
With salary = 135000, 15% is 20250, so the output is:
John Doe
Bonus: $20250
productivity is not used yet. Later versions compare employees by it, so the starter code already declares it.
Approach: percentages with whole numbers
You could write 15% as 0.15, but then the result is a Double such as 20250.0, and you would have to convert it back to a whole number. The lesson avoids that by working with Int only: multiply the salary by 15 first, then divide by 100.
When both sides of / are Int, Scala does integer division: it divides and drops the fraction, so the result is rounded down for positive numbers. That is exactly "rounded down to a whole number". For example, a salary of 99 gives 99 * 15 / 100, which is 1485 / 100, which is 14. A salary of 135001 gives 20250, because 20250.15 loses its fraction.
Step by step
- Compute the bonus. Create a
valnamedbonusfromsalary * 15 / 100. Avalis a value that never changes after it is set, which fits here: the bonus is computed once. - Print the name.
println(name)writes the name and ends the line, so the name is the first line of the output. - Print the bonus line. Join the text
"Bonus: $"andbonuswith+. When one side of+is aString, Scala turns the other side into text, so theIntneeds no conversion.
The solution in Scala
val name = "John Doe"
val productivity = 83
val salary = 135000
val bonus = salary * 15 / 100
println(name)
println("Bonus: $" + bonus)
A few details are worth knowing:
- Order matters in the formula.
salary * (15 / 100)prints 0, because15 / 100is integer division and gives 0 before the multiplication happens. Multiply first, divide last. - The dollar sign is plain text here. In an ordinary string such as
"Bonus: $",$has no special meaning. It only becomes special in an interpolated string that starts withs, where you would write$$to get one dollar sign:s"Bonus: $$${bonus}"prints the same line. - A
valcan't be reassigned. Writingbonus = 2afterwards is a compile error:Reassignment to val bonus. Usevaronly for values that must change.
Edge cases and common mistakes
- Using
0.15.135000 * 0.15is theDouble20250.0, so printing it givesBonus: $20250.0, which doesn't match the expected line. - Rounding. Integer division cuts the fraction off, it doesn't round to the nearest number. A salary of 100 gives a bonus of exactly 15, and 99 gives 14.
- Overflow.
Intis 32 bits and wraps silently. A salary of 150000000 times 15 is more than 2147483647, so150000000 * 15 / 100prints-20449672. The lesson's salaries are far below that limit. For huge amounts, useLong: with150000000Lthe result is 22500000. - Printing in one call. The two lines can come from two
printlncalls, as here. Each call ends its line on its own.
Key takeaways
- Between two
Intvalues,/drops the fraction. - Multiply by the percentage first, then divide by 100.
+joins aStringand anIntby converting the number to text.- The next version builds on this one.
