Start learning free

LESSON 53 OF 671 · CALCULATIONS - CHALLENGES

Employee Of The Year V1 in Java: One Employee

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

String name = "John Doe";
int productivity = 83;
int salary = 135000;

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

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

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

String name = "John Doe";
int productivity = 83;
int salary = 135000;

int bonus = salary * 15 / 100;
System.out.println(name);
System.out.println("Bonus: $" + bonus);

STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Java, explained

    Problem overview

    This first version of the project works with a single employee. You start with three variables: name, productivity and salary. The program computes a bonus of 15% of the salary, rounded down to a whole number, and prints two lines: the employee's name, then Bonus: $ followed by the amount.

    For John Doe with a salary of 135000, 15% is 20250, so the output is:

    John Doe
    Bonus: $20250

    The variable productivity is not used yet. It stays in the starter code because the following versions of the project need it.

    Approach: integer math in one line

    A percentage is "multiply, then divide by 100". With int values, Java's / divides and drops the fraction, so salary * 15 / 100 is already a whole number rounded down. No Math.floor and no cast are needed. Because the salary and the bonus are whole dollar amounts, an int is the natural type for both.

    Step by step

    1. Compute the bonus. Create an int named bonus from salary, multiplied by 15 and then divided by 100.
    2. Print the name on its own line with System.out.println.
    3. Print the bonus line. Join the text "Bonus: $" and the number with +. When one side of + is a String, Java converts the other side to text, so the int needs no conversion. The $ is just a character inside the quotes.

    The solution in Java

    String name = "John Doe";
    int productivity = 83;
    int salary = 135000;
    
    int bonus = salary * 15 / 100;
    System.out.println(name);
    System.out.println("Bonus: $" + bonus);
    

    println prints its argument and then ends the line, so the two calls produce two lines.

    Edge cases and common mistakes

    Order of the operations

    Java evaluates salary * 15 / 100 from left to right. Writing salary * (15 / 100) looks equivalent, but 15 / 100 is integer division and equals 0, so the bonus becomes 0. Multiply first, then divide.

    Using a decimal

    The tempting version, int bonus = salary * 0.15;, does not compile. The product is a double, and Java refuses to store it in an int without an explicit cast:

    error: incompatible types: possible lossy conversion from double to int

    Overflow with very large salaries

    An int holds values up to 2147483647, and an overflow wraps around silently. The multiplication by 15 happens before the division, so it overflows for salaries above about 143 million. With a salary of 150000000, the program prints Bonus: $-20449672. Salaries in this lesson are far below that limit, but if you ever need bigger values, use long.

    Printing the name and the bonus on one line

    Both prints use println, which ends the line after printing. System.out.print does not, so replacing the first call with print would glue the name and Bonus: $20250 together on one line. The output needs two lines, so keep println for both.

    The text between the quotes

    The tests compare the output exactly, so the space after the colon in "Bonus: $" matters, and so does the capital B.

    Testing

    Change salary and check the second line. Rounding down shows up as soon as 15% is not a whole number:

    salaryExact 15%Printed bonus
    10015Bonus: $15
    9914.85Bonus: $14
    71.05Bonus: $1
    00Bonus: $0

    The result is cut, never rounded to the nearest dollar, which is exactly what the lesson's rules ask for.

    Key takeaways

    • Between two int values, / drops the fraction.
    • Multiply by the percentage first, then divide by 100.
    • "text" + number builds a String, and the number is converted for you.
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free