Start learning free

LESSON 97 OF 671 · CONDITIONAL STATEMENTS - CHALLENGES

Employee Of The Year V3 in Java: Three Employees, else if

Practice Read the full explanation

Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.

You start with:

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;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare String winnerName; int winnerSalary; before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

Dividing two whole numbers (int) drops the fraction, so winnerSalary * 15 / 100 is already rounded down: int bonus = winnerSalary * 15 / 100;

+ turns the int into text: System.out.println("Bonus: $" + bonus);

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);

STEP 1 OF 13
    Ctrl + Enter to check

    JAVA · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Java, explained

    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:

    1. Is employee 1 at least as productive as both others? If so, employee 1 wins.
    2. Otherwise employee 1 is out. Is employee 2 at least as productive as employee 3? If so, employee 2 wins.
    3. 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

    1. Declare winnerName and winnerSalary before the chain, without values, so the lines after the chain can read them.
    2. Write the first condition: productivity1 is at least productivity2 and at least productivity3. Join the two comparisons with &&, because employee 1 has to beat both.
    3. Store employee 1's name in winnerName inside that branch.
    4. Store employee 1's salary in winnerSalary.
    5. Add an else if that checks productivity2 >= productivity3.
    6. Store employee 2's name in winnerName.
    7. Store employee 2's salary in winnerSalary.
    8. Add the final else. It needs no condition: if employees 1 and 2 both lost, employee 3 is the winner.
    9. Store employee 3's name in winnerName.
    10. Store employee 3's salary in winnerSalary.
    11. Compute bonus as winnerSalary * 15 / 100.
    12. Print the winner's name.
    13. Print the bonus line with "Bonus: $" and bonus.

    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, 97B
    100, 90, 100A (ties with C, listed first)
    90, 100, 100B (ties with C, listed first)
    100, 100, 100A
    1, 2, 3C
    5, 1, 2A

    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 / else chain runs exactly one branch: the first one whose condition is true, or the else.
    • 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.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free