Lab 1 - Kotlin

Objectives

  • Introduction to Kotlin
  • Data types
  • Control Flow
  • Functions
  • Extension

Tasks

All tasks will be solved in the Kotlin Playground.

When a task is done, please share it to the teacher using the Share button located on the left.

  1. Write a Kotlin application that displays your name.
  2. Write a Kotlin function that computes the factorial for n in an iterative way (without recursion).
  3. Modify the previous function in such a way that is is written as a single expression.
  4. Write an operator extension to so that the !n will compute n the factorial of n. (Hint: Operator Overloading)
  5. Write an Int extension function that computes (as Double) the logarithm of its parameter in base this. Use it in two ways:
    1. 2.log(8.0)
    2. 2 log 8.0
  6. Modify the previous task to be able to use the function with Int numbers as well (`2 log 8`)
  7. Make the following example work:
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)
}
mdad/laboratoare/01.txt ยท Last modified: 2020/10/19 14:47 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0