Problem overview
Two employees are up for the award, each with a name, a productivity score and a salary. The program picks the one with the higher productivity, then prints that employee's name and bonus. The bonus is 15% of the winner's salary, rounded down. Version 1 did this for a single employee; this version adds the decision.
In the lesson's data, John Doe has productivity 83 and a salary of 135000, and Mary Jane has productivity 133 and a salary of 335000. Mary Jane is more productive, and 15% of 335000 is 50250:
Mary Jane
Bonus: $50250
If both employees have the same productivity, the first one (John Doe) wins.
Approach
The bonus formula doesn't change. What changes is whose salary it uses, and whose name is printed. So the program needs to decide once, remember the winner's name and salary, and then run the rest of the code on those two values.
That calls for two variables, winnerName and winnerSalary, and an if/else that fills them. After the if/else, the code that calculates and prints doesn't need to know which branch ran.
Step by step
- Declare the two winner variables without a value.
winnerNameis aStringandwinnerSalaryis anInt. Because there is no initial value, Kotlin needs the type written out. Declare them before theif: a variable created inside the braces would disappear at the closing brace, and the lines after theifneed it. - Write the condition.
productivity1 >= productivity2is true when employee 1 is at least as productive. Using>=rather than>is what sends a tie to employee 1. - Fill both variables in the
ifbranch with employee 1's name and salary. The two values always go together. - Add the
elsebranch, which runs only when employee 2 is strictly more productive. - Fill both variables in the
elsebranch with employee 2's name and salary. - Calculate the bonus after the
if/else, fromwinnerSalary, with the same integer formula as version 1. - Print the name, then the bonus line.
The solution in Kotlin
val name1 = "John Doe"
val productivity1 = 83
val salary1 = 135000
val name2 = "Mary Jane"
val productivity2 = 133
val salary2 = 335000
var winnerName: String
var winnerSalary: Int
if (productivity1 >= productivity2) {
winnerName = name1
winnerSalary = salary1
} else {
winnerName = name2
winnerSalary = salary2
}
val bonus = winnerSalary * 15 / 100
println(winnerName)
println("Bonus: $" + bonus)
Kotlin details worth knowing:
- Declaring without a value.
var winnerName: Stringis allowed, but you can't read the variable until every path has assigned it. If theelsewere missing and the condition were false,println(winnerName)would be rejected witherror: variable 'winnerName' must be initialized.Here both branches assign both variables, so the compiler accepts the code. - Comparison operators.
>=means "greater than or equal",>means "strictly greater". The one-character difference decides who wins a tie. - Integer division.
winnerSalary * 15 / 100multiplies first and divides second, and dividing twoIntvalues drops the fraction. - The dollar sign. A
$followed directly by a closing quote is printed as an ordinary character, so"Bonus: $"is a valid string.
Following the code on the lesson's data
The condition compares 83 with 133. 83 is not at least 133, so the if block is skipped and the else block runs. It stores "Mary Jane" in winnerName and 335000 in winnerSalary. The bonus is 335000 * 15 / 100, which is 50250, and the two println calls print the name and then Bonus: $50250.
An alternative: if as an expression
In Kotlin, if/else produces a value, so each variable can be set in one line:
val winnerName = if (productivity1 >= productivity2) name1 else name2
val winnerSalary = if (productivity1 >= productivity2) salary1 else salary2
With the lesson's data this gives Mary Jane and 335000. Both variables become val, and Kotlin infers their types. The cost is that the comparison is written twice. The lesson's version, with two var declarations and a block per employee, keeps the name and salary assignments side by side, and it is the one the steps follow. Expression form is the usual choice once you only need one value from the decision.
Edge cases and common mistakes
- Ties. With both productivities at 120, the condition
120 >= 120is true and John Doe wins, printingBonus: $20250. Writing>instead would give the tie to Mary Jane and printBonus: $50250, so the lesson's tests would fail. - Declaring inside the branches. A
val bonusorvar winnerNamecreated inside anifblock exists only inside it. Declare what you need after the block before theif. - Mixing employees. The name and the salary must come from the same employee. Taking the name from employee 1 and the salary from employee 2 prints a believable but wrong answer.
- Calculating the bonus in both branches. It works, but it repeats the formula. Calculating once after the
if/elsekeeps one copy. - Comparing the wrong values. The condition compares productivities, not salaries.
Testing
Change the productivity values in the editor above and check the output:
| productivity1 | productivity2 | output |
|---|---|---|
| 83 | 133 | Mary Jane, Bonus: $50250 |
| 140 | 133 | John Doe, Bonus: $20250 |
| 120 | 120 | John Doe, Bonus: $20250 |
