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
- Compute the bonus. Create an
intnamedbonusfromsalary, multiplied by 15 and then divided by 100. - Print the name on its own line with
System.out.println. - Print the bonus line. Join the text
"Bonus: $"and the number with+. When one side of+is aString, Java converts the other side to text, so theintneeds 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:
salary | Exact 15% | Printed bonus |
|---|---|---|
| 100 | 15 | Bonus: $15 |
| 99 | 14.85 | Bonus: $14 |
| 7 | 1.05 | Bonus: $1 |
| 0 | 0 | Bonus: $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
intvalues,/drops the fraction. - Multiply by the percentage first, then divide by 100.
"text" + numberbuilds aString, and the number is converted for you.
