Problem overview
Now two employees compete for the award. The starter code holds name1, productivity1 and salary1 for John Doe, and the same three variables with a 2 for Mary Jane. The more productive employee wins, and the program prints the winner's name and their bonus: 15% of their salary, rounded down.
John has productivity 83 and Mary has 133, so Mary wins. Her bonus is 335000 * 15 / 100 = 50250:
Mary Jane
Bonus: $50250
If both have the same productivity, the employee listed first wins.
Approach: choose the winner, then compute once
The bonus formula is the same for both employees, so the program should not duplicate it. Instead, an if/else copies the winner's name and salary into two shared variables, winnerName and winnerSalary. After the if/else, the code computes the bonus and prints, and it doesn't care who won. Adding more employees later only grows the choosing part.
The variables are declared before the if, without a value. A variable declared inside the braces of a branch exists only until the closing brace, so the prints after the if/else could not see it.
Step by step
- Declare the two blanks: a
String winnerNameand anint winnerSalary, with no value yet. - Open the comparison with
if (productivity1 >= productivity2). It is>=, not>, so that a tie goes to employee 1. - In the
ifbranch, store employee 1's name inwinnerName. - Store employee 1's salary in
winnerSalary. The name and the salary always travel together. - Add the
else. It runs when employee 2 is strictly more productive. - In the
elsebranch, store employee 2's name inwinnerName. - Store employee 2's salary in
winnerSalary. - Compute
bonusbelow theif/elseaswinnerSalary * 15 / 100. Integer division rounds the result down. - Print the winner's name on its own line.
- Print
"Bonus: $"followed bybonus, joined with+.
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 winnerName;
int winnerSalary;
if (productivity1 >= productivity2) {
winnerName = name1;
winnerSalary = salary1;
} else {
winnerName = name2;
winnerSalary = salary2;
}
int bonus = winnerSalary * 15 / 100;
System.out.println(winnerName);
System.out.println("Bonus: $" + bonus);
Java's compiler tracks whether a local variable is certain to have a value before it is read. Because both branches assign winnerName and winnerSalary, they count as assigned after the if/else, and the prints compile.
Edge cases and common mistakes
A tie
With productivity 100 for both employees, >= picks employee 1, so the program prints John Doe and Bonus: $20250. Using > would send the tie to Mary Jane and print Bonus: $50250, which breaks the rule that the first employee wins ties.
Forgetting a branch
If a branch forgets to assign one of the variables, the code does not compile. For example, a String w; assigned only inside an if without an else and then printed produces:
error: variable w might not have been initialized
This check is helpful: it points at the branch you forgot. Giving the variables a made-up default value such as "" or 0 would silence it, but could hide the same bug.
Declaring inside the braces
Writing String winnerName = name1; inside the if creates a variable that disappears at the closing brace. If it was not declared before the if, the later println(winnerName) fails with a "cannot find symbol" error. If it was declared before, redeclaring it inside the branch is also a compile error, because the name is already taken.
Two separate if statements
Writing a second if (productivity2 > productivity1) instead of an else also works for two employees, but the compiler can no longer tell that one of the two assignments always happens, so it reports the same "might not have been initialized" error. else says "everything the if did not catch", which is exactly what is needed here.
Mixing the employees
The winner's name and the winner's salary must come from the same employee. Taking the name from employee 2 and the salary from employee 1 gives the right name next to the wrong bonus.
Testing
Only one branch runs each time, so one run never covers both of them.
Edit the starter values to follow each path through the if/else:
- John Doe with productivity 200 against Mary Jane with 133: the
ifbranch runs, and the output isJohn DoeandBonus: $20250. - Equal productivity (100 and 100): the
ifbranch runs because of>=, and the output is againJohn DoeandBonus: $20250. - Salaries 100 and 99 with productivities 1 and 2: the
elsebranch runs, and the bonus is 99 * 15 / 100, which prints asBonus: $14after rounding down.
Running both branches is worth the effort. A mistake in the else branch, such as a copy-paste that still says name1, only shows when employee 2 wins.
Key takeaways
- Declare a variable before an
if/elsewhen code after the block needs it. - Use
>=to give ties to the employee listed first. - Choose first, then compute once: the shared code after the
if/elseworks for either winner.
