Problem overview
Two variables are already declared: width, which is 23, and height, which is 17. The task is to print the area of the rectangle they describe. The area is the width multiplied by the height, so the program has to compute 23 * 17 and display it, using the variables rather than the typed-in numbers.
Approach
This lesson needs one arithmetic operator, *, and one output function, println. A rectangle's area is width times height, and the multiplication can sit directly inside the call. There is no reason to store the area in a variable first, because the value is used once and then forgotten. Both variables are Int values, and the product of two Int values is an Int, so the output has no decimal point.
Step by step
- Start from the two declarations. They already give
widthandheighttheir values, so the new code goes after them, on the line that follows. - Multiply the variables. Write
width * height. Kotlin replaces each name with its value, so this is 23 times 17. - Pass the product to
println. The function prints whatever the expression evaluates to and ends the line.
The solution in Kotlin
val width: Int = 23
val height: Int = 17
println(width * height)
The program prints 391. A few details are worth knowing:
- Use the variables, not the numbers. Writing
println(391)would print the right answer for this input only. Withwidth * height, changing a declaration changes the output automatically. valmeans the variable can't be reassigned. That is the right choice here, since the dimensions never change.- The type is inferred if you leave it out.
val width = 23is also anInt. The lesson's: Intjust spells it out. - Integer results stay integers. If the dimensions were
Doublevalues such as2.5and4.0, the same expression would print10.0.
Going a little further
Once the area works, two small variations show how the same idea scales.
Storing the result
If you want to use the area again, put it in a variable and print the variable. A string template, written with $ inside double quotes, lets you add a label:
val width = 23
val height = 17
val area = width * height
println("Area: $area")
println("Area: ${width * height}")
Both lines print Area: 391. A plain name after $ inserts its value, and an expression needs braces: ${...}. The lesson itself needs only the bare number, so keep the output exactly 391 there.
Other numeric types
The type of the product follows the types of the operands. An Int times a Double is a Double: with val w = 2.5 and val h = 4, the expression w * h prints 10.0. An Int is also limited to about 2.1 billion, so very large dimensions overflow without any warning:
val bigW = 100000
val bigH = 100000
println(bigW * bigH) // 1410065408
println(bigW.toLong() * bigH) // 10000000000
Converting one operand to Long before multiplying fixes it. The numbers in this lesson are small, so plain Int is fine.
Edge cases and common mistakes
- Adding instead of multiplying.
width + heightgives 40, which is half the perimeter, not the area. - Printing the variable names.
println("width * height"), with quotes, prints the textwidth * height. Without quotes, Kotlin evaluates the expression. - Writing the code before the declarations. Kotlin reads top to bottom, so a variable must be declared before the line that uses it.
- Using
xfor multiplication. The operator is the asterisk,*.
Testing
Try other values in the declarations and compare the output with a hand calculation:
width = 5,height = 4prints20.width = 1,height = 100prints100.width = 0,height = 9prints0.
Key takeaways
- Put the calculation inside
println; no helper variable is required. - Use the variables so the program still works when their values change.
- The type of a product follows its operands:
InttimesIntis anInt, and aDoubleanywhere makes the result aDouble. - Declarations come before the lines that use them, because Kotlin reads from top to bottom.