Start learning free

LESSON 96 OF 671 · CONDITIONAL STATEMENTS - CHALLENGES

Employee Of The Year V2 in Java: Two Employees with if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute 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;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (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 if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

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

STEP 1 OF 10
    Ctrl + Enter to check

    JAVA · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Java, explained

    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

    1. Declare the two blanks: a String winnerName and an int winnerSalary, with no value yet.
    2. Open the comparison with if (productivity1 >= productivity2). It is >=, not >, so that a tie goes to employee 1.
    3. In the if branch, store employee 1's name in winnerName.
    4. Store employee 1's salary in winnerSalary. The name and the salary always travel together.
    5. Add the else. It runs when employee 2 is strictly more productive.
    6. In the else branch, store employee 2's name in winnerName.
    7. Store employee 2's salary in winnerSalary.
    8. Compute bonus below the if/else as winnerSalary * 15 / 100. Integer division rounds the result down.
    9. Print the winner's name on its own line.
    10. Print "Bonus: $" followed by bonus, 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 if branch runs, and the output is John Doe and Bonus: $20250.
    • Equal productivity (100 and 100): the if branch runs because of >=, and the output is again John Doe and Bonus: $20250.
    • Salaries 100 and 99 with productivities 1 and 2: the else branch runs, and the bonus is 99 * 15 / 100, which prints as Bonus: $14 after 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/else when 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/else works for either winner.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free