This shows you the differences between two versions of the page.
mdad:laboratoare:01 [2020/10/19 11:35] alexandru.radovici |
mdad:laboratoare:01 [2020/10/19 14:47] (current) ioana_maria.culic [Tasks] |
||
---|---|---|---|
Line 16: | Line 16: | ||
- Write a Kotlin function that computes the factorial for n in an iterative way (without recursion). | - Write a Kotlin function that computes the factorial for n in an iterative way (without recursion). | ||
- Modify the previous function in such a way that is is written as a single expression. | - Modify the previous function in such a way that is is written as a single expression. | ||
- | - Write an Int extension function that computes (as Double) the logarithm of its parameter in base `this`. Use it in two ways: | + | - Write an operator extension to so that the ''!n'' will compute n the factorial of n. (Hint: [[https://kotlinlang.org/docs/reference/operator-overloading.html|Operator Overloading]]) |
- | - `2.log(8.0) | + | - Write an Int extension function that computes (as Double) the logarithm of its parameter in base ''this''. Use it in two ways: |
- | - `2 log 8.0` | + | - ''2.log(8.0)'' |
+ | - ''2 log 8.0'' | ||
- Modify the previous task to be able to use the function with Int numbers as well (`2 log 8`) | - Modify the previous task to be able to use the function with Int numbers as well (`2 log 8`) | ||
+ | - Make the following example work: | ||
+ | <code kotlin> | ||
+ | data class Complex (var r: Double, var i: Double) | ||
+ | |||
+ | fun main () { | ||
+ | val a = Complex (1.0,2.0) | ||
+ | val b = Complex (3.0,4.0) | ||
+ | |||
+ | var s1 = a + b | ||
+ | var s2 = a - b | ||
+ | var s3 = a * b | ||
+ | var s4 = a == b | ||
+ | println (s1) | ||
+ | println (s2) | ||
+ | println (s3) | ||
+ | println (s4) | ||
+ | } | ||
+ | </code> |