Start learning free

LESSON 54 OF 291 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in Kotlin: One Employee's Bonus

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:

        val name = "John Doe"
        val productivity = 83
        val 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: val bonus = salary * 15 / 100

+ turns the Int into text: println("Bonus: $" + bonus)

        val name = "John Doe"
        val productivity = 83
        val salary = 135000

        val bonus = salary * 15 / 100
        println(name)
        println("Bonus: $" + bonus)

STEP 1 OF 3
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Kotlin, explained

    Problem overview

    This is the first version of a small project. You have one employee, described by three values: name, productivity and salary. The program must work out the employee's end-of-year bonus and print two lines: the name, then the bonus.

    The bonus is 15% of the salary, rounded down to a whole number. With name = "John Doe" and salary = 135000, 15% of 135000 is 20250, so the program prints:

    John Doe
    Bonus: $20250
    

    The productivity value is in the starter code but this version doesn't use it. It is there because the next version builds on this one.

    Approach

    There are three small jobs: calculate the bonus, print the name, and print the bonus with its label. The calculation is the only part that needs thought, because the result has to be a whole number.

    The obvious way to take 15% of a number is to multiply by 0.15. That produces a Double, and you would then have to convert it back to an Int. Integer arithmetic avoids that: multiply the salary by 15 first, then divide by 100. In Kotlin, dividing one Int by another gives an Int and drops the fraction, so the rounding down happens by itself.

    Step by step

    1. Create bonus. Declare it with val, because the value never changes after it is calculated. Its value is salary * 15 / 100. Kotlin evaluates * and / from left to right, so the multiplication happens first.
    2. Print the name. println(name) prints the text and ends the line, so the name sits alone on the first line.
    3. Print the bonus line. Join the label "Bonus: $" and the number with +. When the left side of + is a String, Kotlin turns the Int on the right into text.

    The solution in Kotlin

            val name = "John Doe"
            val productivity = 83
            val salary = 135000
    
            val bonus = salary * 15 / 100
            println(name)
            println("Bonus: $" + bonus)
    

    A few Kotlin details matter here:

    • Integer division truncates. With salary = 135050 the exact bonus is 20257.5, and salary * 15 / 100 gives 20257. Multiplying by 0.15 instead gives the Double 20257.5; you would need .toInt() to get a whole number.
    • Order matters. salary / 100 * 15 divides first, throwing away the remainder of the salary, and can give a smaller bonus. With salary = 135050 it gives 20250, not 20257. Multiply first, then divide.
    • The dollar sign. In a Kotlin string, $ starts a template such as "$bonus". A $ followed by a quote, as in "Bonus: $", has nothing to substitute, so it is printed as a plain character. If you want a dollar sign directly before a template, write \$: "Bonus: \$$bonus" prints Bonus: $20257 for salary = 135050.
    • String + Int works; Int + String does not. "Bonus: $" + bonus compiles, but bonus + "Bonus: $" is a compile error, because Int's plus only accepts numbers.
    • val, not var. Use var only for a variable that changes later.
    • productivity stays unused. That is fine here. Leave the line as it is.

    Edge cases and common mistakes

    • Overflow. An Int is 32 bits, and overflow wraps around silently. salary * 15 overflows once the salary is above about 143 million. With salary = 150000000, salary * 15 / 100 prints -20449672. If salaries could be that large, use Long for the arithmetic: salary.toLong() * 15 / 100 gives 22500000. The lesson's salary is far below the limit, so plain Int is fine.
    • Floating-point rounding. Going through Double and truncating is easy to get subtly wrong, and the integer formula needs no conversion.
    • Wrong output text. The tests compare the text exactly, so Bonus: $20250 needs the capital B, the colon, one space and the dollar sign with no space after it.
    • Two println calls, not one. Each call ends its line, which gives the two lines the output needs.

    Testing

    Try a few salaries in the editor above. The first row is the lesson's own data; the others come from running the same code with a different salary:

    salaryprinted bonus line
    135000Bonus: $20250
    135050Bonus: $20257
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free