Problem overview
Three employees now compete: John Doe (productivity 83), Mary Jane (133) and Bob Smith (97). The program must find the one with the highest productivity and print their name, then their 15% bonus rounded down. When several employees share the highest productivity, the one listed first wins.
Mary has the highest productivity, and 335000 * 15 / 100 is 50250:
Mary Jane
Bonus: $50250
The previous version compared two employees with if/else. This one needs a chain: if, else if, else.
Approach: ask "does employee 1 win?", then narrow down
With three employees, a single comparison can't decide. The chain asks questions in order, and each one runs only if all the earlier ones were false:
- Is employee 1 at least as productive as both others? If so, employee 1 wins.
- Otherwise employee 1 is out. Is employee 2 at least as productive as employee 3? If so, employee 2 wins.
- Otherwise employee 3 wins.
The chain never compares employee 2 with employee 1 again. By the time the else if runs, employee 1 is known to have lost, so only employees 2 and 3 are left. That is why the second condition is short.
As before, the chain only fills in winnerName and winnerSalary, and the bonus code after it is written once.
Step by step
- Declare
winnerNameandwinnerSalarybefore the chain, without values, so the lines after the chain can read them. - Write the first condition:
productivity1is at leastproductivity2and at leastproductivity3. Join the two comparisons with&&, because employee 1 has to beat both. - Store employee 1's name in
winnerNameinside that branch. - Store employee 1's salary in
winnerSalary. - Add an
else ifthat checksproductivity2 >= productivity3. - Store employee 2's name in
winnerName. - Store employee 2's salary in
winnerSalary. - Add the final
else. It needs no condition: if employees 1 and 2 both lost, employee 3 is the winner. - Store employee 3's name in
winnerName. - Store employee 3's salary in
winnerSalary. - Compute
bonusaswinnerSalary * 15 / 100. - Print the winner's name.
- Print the bonus line with
"Bonus: $"andbonus.
The solution in Java
String name1 = "John Doe";
int productivity1 = 83;
int salary1 = 135000;
String name2 = "Mary Jane";
int productivity2 = 133;
int salary2 = 335000;
String name3 = "Bob Smith";
int productivity3 = 97;
int salary3 = 210000;
String winnerName;
int winnerSalary;
if (productivity1 >= productivity2 && productivity1 >= productivity3) {
winnerName = name1;
winnerSalary = salary1;
} else if (productivity2 >= productivity3) {
winnerName = name2;
winnerSalary = salary2;
} else {
winnerName = name3;
winnerSalary = salary3;
}
int bonus = winnerSalary * 15 / 100;
System.out.println(winnerName);
System.out.println("Bonus: $" + bonus);
Java checks the branches from top to bottom and runs only the first one whose condition is true; the rest are skipped. The && operator also stops early: if the left comparison is false, the right one is not evaluated.
Edge cases and common mistakes
Ties
Every comparison uses >=, so the earlier employee wins a tie. The table shows what the chain picks for different productivities, using the same logic with employees A, B and C:
| Productivities (1, 2, 3) | Winner |
|---|---|
| 83, 133, 97 | B |
| 100, 90, 100 | A (ties with C, listed first) |
| 90, 100, 100 | B (ties with C, listed first) |
| 100, 100, 100 | A |
| 1, 2, 3 | C |
| 5, 1, 2 | A |
Comparing only neighbours
A frequent mistake is the first condition productivity1 >= productivity2 alone. With productivities 100, 90 and 120, employee 1 beats employee 2 and wins, even though employee 3 is better. Employee 1 must be compared with both others.
Using > instead of >=
With > in the first condition, the case 100, 90, 100 would fail it (100 is not greater than 100), and the chain would move on and pick employee 3, not employee 1.
Separate if statements
Writing three independent if statements instead of a chain lets several branches run, and a later one overwrites an earlier result. else if guarantees that exactly one branch runs.
Testing
Follow the starter data through the chain. Employee 1 has productivity 83: is 83 at least 133? No, so the first condition is false (and the second half, 83 against 97, is never even looked at). The else if asks whether 133 is at least 97. It is, so Mary Jane is chosen, and the else never runs.
To test the other branches, change the productivity numbers. Make productivity1 200 to reach the first branch, which prints John Doe. Make productivity3 200 to reach the final else, which prints Bob Smith and a bonus of 210000 * 15 / 100 = 31500. Test the ties from the table too: a tie is where >= and > differ.
Reading the results
Each branch of the chain should be reached at least once during testing.
The three runs described above print, in order: Mary Jane with Bonus: $50250; John Doe with Bonus: $20250; and Bob Smith with Bonus: $31500. If a branch prints the wrong person, the usual cause is a name or salary line copied from the branch above and not updated.
Key takeaways
- An
if / else if / elsechain runs exactly one branch: the first one whose condition is true, or theelse. - Use
&&to require two conditions at once. - Each later condition can rely on the earlier ones having been false, which is why the chain gets shorter as it goes.
