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
314Comments
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.Geometry.circle-area(10)calls the method on the type object, passing10. The result isπ * 10 * 10, and after rounding314.