Start learning free

LESSON 54 OF 291 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in Scala: One 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

    SCALA · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Scala, explained

    Problem overview

    This is the first version of a small project. The program holds the data of one employee in three values: name, productivity and salary. It must print the name, then the end-of-year bonus, which is 15% of the salary rounded down to a whole number.

    With salary = 135000, 15% is 20250, so the output is:

    John Doe
    Bonus: $20250
    

    productivity is not used yet. Later versions compare employees by it, so the starter code already declares it.

    Approach: percentages with whole numbers

    You could write 15% as 0.15, but then the result is a Double such as 20250.0, and you would have to convert it back to a whole number. The lesson avoids that by working with Int only: multiply the salary by 15 first, then divide by 100.

    When both sides of / are Int, Scala does integer division: it divides and drops the fraction, so the result is rounded down for positive numbers. That is exactly "rounded down to a whole number". For example, a salary of 99 gives 99 * 15 / 100, which is 1485 / 100, which is 14. A salary of 135001 gives 20250, because 20250.15 loses its fraction.

    Step by step

    1. Compute the bonus. Create a val named bonus from salary * 15 / 100. A val is a value that never changes after it is set, which fits here: the bonus is computed once.
    2. Print the name. println(name) writes the name and ends the line, so the name is the first line of the output.
    3. Print the bonus line. Join the text "Bonus: $" and bonus with +. When one side of + is a String, Scala turns the other side into text, so the Int needs no conversion.

    The solution in Scala

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

    A few details are worth knowing:

    • Order matters in the formula. salary * (15 / 100) prints 0, because 15 / 100 is integer division and gives 0 before the multiplication happens. Multiply first, divide last.
    • The dollar sign is plain text here. In an ordinary string such as "Bonus: $", $ has no special meaning. It only becomes special in an interpolated string that starts with s, where you would write $$ to get one dollar sign: s"Bonus: $$${bonus}" prints the same line.
    • A val can't be reassigned. Writing bonus = 2 afterwards is a compile error: Reassignment to val bonus. Use var only for values that must change.

    Edge cases and common mistakes

    • Using 0.15. 135000 * 0.15 is the Double 20250.0, so printing it gives Bonus: $20250.0, which doesn't match the expected line.
    • Rounding. Integer division cuts the fraction off, it doesn't round to the nearest number. A salary of 100 gives a bonus of exactly 15, and 99 gives 14.
    • Overflow. Int is 32 bits and wraps silently. A salary of 150000000 times 15 is more than 2147483647, so 150000000 * 15 / 100 prints -20449672. The lesson's salaries are far below that limit. For huge amounts, use Long: with 150000000L the result is 22500000.
    • Printing in one call. The two lines can come from two println calls, as here. Each call ends its line on its own.

    Key takeaways

    • Between two Int values, / drops the fraction.
    • Multiply by the percentage first, then divide by 100.
    • + joins a String and an Int by converting the number to text.
    • The next version builds on this one.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free