-
Notifications
You must be signed in to change notification settings - Fork 2
homework
Share your code with user fmcarvalho
in a private Github repository named isel-lae-2122-your_number
.
Implement the Point.java
(equivalent to Point.kt
) and use it in an App.kt
program.
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()}")
} |
Implement a fun inspectKlass(klass: KClass<*>)
that prints the qualified name of klass
followed by its properties and member functions.
-
For each function it should print its return type and parameters.
-
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 withcreateInstance()
ofKClass
and pass it tocall()
.
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.
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())
}
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
Given the eager implementation of concat()
, implement a new extension
function:
-
concatLazy()
that returns a lazy implementation ofIterable<T>
-
concat()
that returns a lazy implementation ofSequence<T>
taking advantage of the auxiliary kotlin functionsequence
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
}