Problem overview
Two employees compete for the award. Each has a name, a productivity score and a salary, held in six values: name1, productivity1, salary1 and the same three with a 2. The program picks the employee with the higher productivity, then prints their name and their bonus, which is 15% of their salary rounded down. On a tie, employee 1, the one listed first, wins.
With John Doe at productivity 83 and Mary Jane at 133, Mary Jane wins. Her salary is 335000, and 335000 * 15 / 100 is 50250:
Mary Jane
Bonus: $50250
The previous version computed the bonus for one employee. This one adds the decision of who gets it.
Approach: store the winner, then compute once
The bonus formula is the same for both employees, so the program shouldn't repeat it in each branch. Instead the if/else only records who won, by copying the winner's name and salary into two variables. After the if/else, one block of code computes the bonus from winnerSalary and prints it.
The tie rule decides the comparison. Employee 1 should win when the productivities are equal, so the if tests productivity1 >= productivity2. Everything else, meaning employee 2 is strictly more productive, falls to the else.
Why else and not a second if
You could write two separate if statements, one for each employee. That makes the program ask the second question even when the first one already settled the winner, and with ties you would have to make sure both conditions can't be true at once. With if/else exactly one branch runs, so there is always exactly one winner and the tie rule lives in a single comparison. It also leaves no gap: whatever the two numbers are, one of the branches is taken.
Step by step
- Declare the two blanks. Create
var winnerName: String = ""andvar winnerSalary: Int = 0before theif. They arevars because they get a new value later. They go before theifbecause a variable declared inside braces exists only inside them. - Write the condition.
if (productivity1 >= productivity2) {, and close the wholeif/elsewith its}right away so the braces stay balanced while you fill it in. - Employee 1's branch. Inside the
if, assignwinnerName = name1andwinnerSalary = salary1. They go together: the bonus needs the salary of the same person whose name is printed. - The
else. Add} else {. It runs only when employee 2 is more productive. - Employee 2's branch. Assign
winnerName = name2andwinnerSalary = salary2. - The bonus. After the
if/else,val bonus = winnerSalary * 15 / 100. Both operands areInt, so/drops the fraction. - Print.
println(winnerName), thenprintln("Bonus: $" + bonus).
The solution in Scala
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 = 0
if (productivity1 >= productivity2) {
winnerName = name1
winnerSalary = salary1
} else {
winnerName = name2
winnerSalary = salary2
}
val bonus = winnerSalary * 15 / 100
println(winnerName)
println("Bonus: $" + bonus)
Notes on the Scala details:
- Scope. If you declare
winnerNameinside theifandelseblocks and print it afterwards, the compiler reportsNot found: winnerName, because each block has its own scope. ifis an expression. In Scala anif/elseproduces a value, so the same logic can be written withoutvar:val (n, s) = if (productivity1 >= productivity2) (name1, salary1) else (name2, salary2). The lesson uses the assignment form to practisevarand branches; both give the same result.- Parentheses. The condition goes in parentheses, and the braces are needed as soon as a branch holds more than one statement.
Edge cases and common mistakes
- Ties. With both productivities at 133 the program prints
John DoeandBonus: $20250. Using>instead of>=would hand the tie to Mary Jane. - Employee 1 wins. With productivities 140 and 133 the output is
John DoeandBonus: $20250. - Mixing employees. Setting
winnerName = name2butwinnerSalary = salary1prints one person with the other's bonus. Always change both together. - Unbalanced braces. Every
{needs its}. The usual layout puts theelseon the same line as the brace that closes theifbody, as in} else {, which makes it easy to see which branch each block belongs to. If the closing braces are missing or misplaced, the program doesn't compile. - Copy-pasted formula. Computing the bonus inside each branch works but repeats the formula, so one fix has to be made twice.
Following the program
With the starter data, productivity1 >= productivity2 asks whether 83 is at least 133. It is not, so the else branch runs and the two variables change like this:
| Moment | winnerName | winnerSalary |
|---|---|---|
| After the declarations | empty text | 0 |
After the else branch | Mary Jane | 335000 |
Only then does the bonus line run, with 335000 * 15 = 5025000 and 5025000 / 100 = 50250. The placeholders from the declarations never reach the output: exactly one branch always runs, so both variables are always overwritten before they are read.
Testing
Try these inputs by changing the two productivity values (the salaries stay as they are in the starter data):
| productivity1 | productivity2 | Output |
|---|---|---|
| 83 | 133 | Mary Jane, Bonus: $50250 |
| 140 | 133 | John Doe, Bonus: $20250 |
| 133 | 133 | John Doe, Bonus: $20250 |
Key takeaways
- Use
>=when the first item wins ties. - Declare variables before the
ifwhen code after it needs them. - Let the branches choose, and do the shared calculation once afterwards.
- The next version builds on this one.
