Problem overview
This first version of the Employee of the Year project handles a single employee. The starter code gives three constants: name, productivity and salary. The program computes the employee's end-of-year bonus, which is 15% of the salary rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.
With salary = 135000, 15% is 20250, so the program prints:
John Doe
Bonus: $20250
The productivity constant is not used in this version. It is part of the data so the next versions can compare employees; the next version builds on this one.
Approach: do the percentage with whole numbers
A percentage is "multiply by the rate, divide by 100". Swift divides two Int values as integer division: the fraction is cut off, so the result is already rounded down. That is exactly the rounding the rules ask for, and no floating-point type or rounding function is needed.
The order matters. salary * 15 / 100 multiplies first, so the division sees the full product. Writing salary / 100 * 15 would drop the fraction too early: with a salary of 199 it gives 15, while the correct rounded-down bonus is 29.
The program then needs two prints. The name goes out as it is. The bonus is a number, so it has to become text before it can sit next to Bonus: $.
Step by step
- Create
bonuswithlet. The value is computed once and never changes, so a constant fits. It issalary * 15 / 100. - Print the name on its own line.
printadds the line break itself. - Print the bonus line. The text
Bonus: $and the number go into one string literal, with the number inserted by interpolation,\(bonus).
The solution in Swift
let name = "John Doe"
let productivity = 83
let salary = 135000
let bonus = salary * 15 / 100
print(name)
print("Bonus: $\(bonus)")
The Swift details worth knowing:
- Interpolation. Inside a string literal,
\(expression)is replaced by the value's text. It works for any value that can be printed, so no conversion call is needed. The$has no special meaning in Swift strings; it is an ordinary character. - No
IntplusString. Swift won't join a number to text with+. Writing"Bonus: $" + bonusfails to compile:binary operator '+' cannot be applied to operands of type 'String' and 'Int'. Interpolation, or"Bonus: $" + String(bonus), fixes it. - Type inference.
salaryis anIntbecause the literal135000has no decimal point, andbonusis anIntbecause it comes fromIntarithmetic. Ifsalarywere written as135000.0, it would be aDouble, and the division would keep the fraction. - Constants. Everything here is a
let. Swift reports an error if code tries to assign to aleta second time.
Edge cases and common mistakes
- Small salaries round down. With
salary = 99, the product is 1485 and the division gives 14, so the program printsBonus: $14. - Dividing first. As above,
salary / 100 * 15loses the remainder before the multiplication. Multiply first. - Printing both values in one call.
print(name, bonus)would put them on one line, separated by a space. The output needs two separate lines, so there are twoprintcalls. - Using
productivity. It plays no part in the bonus here. Leave the constant as it is.
Testing
salary = 135000printsJohn DoeandBonus: $20250.salary = 99printsJohn DoeandBonus: $14.
