The task
The starter code declares two values, the width and the height of a rectangle. The exercise is to print the rectangle's area. Area is width times height, so the only operator needed is *. With a width of 23 and a height of 17, the area is 391.
How to think about it
Whenever an exercise gives you values in variables, use the variables in the expression instead of copying their numbers into it. If you write println(391), the output is right for this one input, but the code no longer calculates anything. A calculation built from width and height stays correct when the two values change.
Two other details help:
printlnaccepts any expression. Scala calculates the expression first and prints its value, so you do not need to store the area in a new variable. You can still do it if it reads better to you:val area = width * heightand thenprintln(area).- Both values are declared with
val, which means they cannot be reassigned. That is fine here, because nothing needs to change them. The multiplication only reads them.
Step by step
- Find the two variables in the starter code:
widthandheight. Both areInt. - On the next line, write
width * heightto get the area. - Wrap that expression in
println(...)so the result appears on the console.
The solution in Scala
val width: Int = 23
val height: Int = 17
println(width * height)
The program prints:
391
The type Int after each name is optional, because Scala can work out the type from the number. Leaving it out, as in val width = 23, gives the same result. Multiplying two Int values gives an Int, so the output has no decimal point.
Trying other values
Change the two declarations and the same expression follows. With a width of 5 and a height of 4, width * height prints 20. The perimeter would be a different formula, 2 * (width + height), which gives 18 for the same rectangle. The parentheses matter there: without them, 2 * width + height would multiply only the width.
The sides do not have to be whole numbers. If width is the Double value 2.5 and height is 4.0, then width * height prints 10.0. A Double result always prints with a decimal part, even when it is a whole number.
To print a label together with the number, join them with +. For an area of 20, println("Area: " + area) prints Area: 20. The exercise itself expects only the number, so keep the output to the plain value.
Edge cases and common mistakes
- Adding instead of multiplying.
width + heightis half the perimeter, 40 here, not the area. - Printing text instead of a value.
println("width * height")prints those characters exactly as written. Without the quotes, Scala treatswidth * heightas a calculation. - Using other values. The lesson's tests may change the numbers, which is why the expression must use the variable names.
- Very large sizes. An
Intholds values up to 2147483647. Multiplying two largeIntvalues can exceed that, and Scala wraps around silently:2147483647 * 2gives-2. For huge dimensions, declare the variables asLong. The numbers in this exercise are far from that limit.
Key takeaways
- Area of a rectangle is
width * height. - Use the variables, not their current values.
printlnprints the value of any expression you give it.
