Skip to content

homework

Miguel Gamboa edited this page May 19, 2022 · 9 revisions

Share your code with user fmcarvalho in a private Github repository named isel-lae-2122-your_number.

Homework 01 - Kotlin and Java interoperability (7-3-2022)

Implement the Point.java (equivalent to Point.kt) and use it in an App.kt program.

Homework 02 - Kotlin Type System and Java interoperability (8-3-2022)

Consider the following definition of Point class and its client application App, both in Kotlin. Build, run AppKt.class and check its output.

Then, without changing any code of App.kt, replace the Point.kt with a new equivalent implementation of Point.java in Java, which maintains the same output when running AppKt.class.

data class Point(val x: Int, val y: Int) {
    fun distance(other: Point = Point(0,0)) : Double {
        val dX = x - other.x
        val dY = y - other.y
        return sqrt((dX*dX + dY*dY).toDouble())
    }
}
fun main() {
    val p1 = Point(5, 7)
    val p2 = Point(3, 9)
    println("Distance $p1 to $p2 = ${p1.distance(p2)}")
    println("Distance $p1 to (0,0) = ${p1.distance()}")
}

Homework 03 - Kotlin Reflection (14-3-2022)

Implement a fun inspectKlass(klass: KClass<*>) that prints the qualified name of klass followed by its properties and member functions.

  1. For each function it should print its return type and parameters.

  2. For each parameterless function it should present its invocation result through call(). For functions requiring a target instance (i.e. this) you should create an object with createInstance() of KClass and pass it to call().

Homework 04 - Kotlin Reflection, Logger (15-3-2022)

Implement logFunctions(target: Any) of Logger of lesson 6, to print the result of parameterless functions calls on target.

Notice, you should only call functions that are declared in the target class (i.e. not inherited) and without void (i.e. Unit) result.

Your implementation should pass the unit test testLogAccountPropertiesAndFunctions(). Notice that for class SavingsAccount of unit tests, the logger output should include the result of the function call monthlyInterest(), and also balance and annualInterestRate properties, whose getters are also functions.

Homework 05 - LoggerDynamic (20-4-2022)

Complete the implementation of buildGetterProperty(klass: KClass<*>, prop: KProperty<*>) that satisfies the unit test DynamicLoggerTest.kt:

@Test fun testDynamicGetter() {
  val bal: KProperty<*> = SavingsAccount::class
            .declaredMemberProperties
            .find { it.name == "balance" }
            ?: throw Exception("SavingsAccount without a property balance!!!")
  val getterBalanceKlassFile = buildGetterProperty(SavingsAccount::class, bal)
  val out = PrinterStringBuffer()
  val getterBalance = loadAndCreateInstance(getterBalanceKlassFile, out) as Getter
  getterBalance.readAndPrint(SavingsAccount(2733, 2.5))
  assertEquals("balance = 2733,", out.buffer.toString())
}

Homework 06 - Exemplo bytecodes do readAndPrint() (2-5-2022)

public void readAndPrint(java.lang.Object);
    Code:
        aload_1
        checkcast     #3       // class pt/isel/SavingsAccount
        invokevirtual #4       // Method pt/isel/SavingsAccount.getBalance:()J
        invokestatic  #5       // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
        astore_2
        aload_0
        getfield      #2       // Field out:Lpt/isel/Printer;
        aload_2
        invokedynamic #6,  0   // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/Object;)Ljava/lang/String;
        invokeinterface #7,  2 // InterfaceMethod pt/isel/Printer.print:(Ljava/lang/Object;)V
        return

Homework 07 - Lazy sequence operation concat() (19-5-2022)

Given the eager implementation of concat(), implement a new extension function:

  1. concatLazy() that returns a lazy implementation of Iterable<T>

  2. concat() that returns a lazy implementation of Sequence<T> taking advantage of the auxiliary kotlin function sequence

Example of a unit test using concat

/**
 * Eager implementation of concat for Iterable sequences.
 */
fun <T> Iterable<T>.concat(other: Iterable<T>): Iterable<T> {
    val res = mutableListOf<T>()
    for (item in this) { res.add(item) }
    for (item in other) { res.add(item) }
    return res
}