Choosing the right delegate, with a bit of arithmetics mixed in

Report a typo

Complete the code in the main method by providing an instance of the Printer class with the correct delegate from the list below (AdditionPrinter, SubtractionPrinter, or MultiplicationPrinter) so that calling the print() method would print 10 stars.

class AdditionPrinter : MyInterface {
    override fun print() { for (i in 1..amount) print("*") }
    override val amount: Int = 3 + 7 // 10
}

class SubtractionPrinter : MyInterface {
    override fun print() { for (i in 1..amount) print("*") }
    override val amount: Int = 23 - 15 // 8
}

class MultiplicationPrinter : MyInterface {
    override fun print() { for (i in 1..amount) print("*") }
    override val amount: Int = 3 * 4 // 12
}

Hint: 10=8+210 = 8 + 2

Sample Output:

**********
Write a program in Kotlin
interface MyInterface {
fun print()
val amount: Int
}

// Write the correct class here

class Printer(base: MyInterface) : MyInterface by base {
override fun print() {
for (i in 1..(amount + 2)) print("*")
}
}

fun main() {

// Your code here
val p = //?

// Do not change the code below
p.print()
}
___

Create a free account to access the full topic