Start learning free

LESSON 17 OF 414 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in Scala: Multiplying Two Variables

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in Scala, explained

    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:

    • println accepts 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 * height and then println(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

    1. Find the two variables in the starter code: width and height. Both are Int.
    2. On the next line, write width * height to get the area.
    3. 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 + height is 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 treats width * height as 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 Int holds values up to 2147483647. Multiplying two large Int values can exceed that, and Scala wraps around silently: 2147483647 * 2 gives -2. For huge dimensions, declare the variables as Long. 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.
    • println prints the value of any expression you give it.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free