Course of Raku / Objects, I/O, and exceptions / Classes and objects / Class methods / Exercises / A formula on the class

Solution: A formula on the class

Here is a possible solution to the task.

Code

class Geometry {
    method circle-area($r) {
        (π * $r * $r).round
    }
}

say Geometry.circle-area(10);

🦋 You can find the source code in the file speed-limit.raku.

Output

314

Comments

  1. The method does not touch any attribute — it works only with its parameter $r — so it can be called on the class itself, with no object created first.

  2. Geometry.circle-area(10) calls the method on the type object, passing 10. The result is π * 10 * 10, and after rounding 314.

Course navigation

A formula on the class   |   Kilometres to miles