Fresh from the lines

Report a typo

Below is program code, in which the Counter class delegates the implementation of the following Increment interface:

interface Increment {
    fun increment()
}

The Increment interface is delegated to the IncrementHandler class:

class IncrementHandler(private val counter: Counter) : Increment {
    override fun increment() {
        counter.count++
    }
}

Your task is to correctly position the lines of code to make the program run.

fun main() {
    val counter = Counter(0) { IncrementHandler(it) }
    for (i in 1..5) {
        counter.increment()
        println("Count: ${counter.count}")
    }
}
Reorder lines using drag or arrows. Adjust indentation with left buttons
                class Counter(var count: Int, incrementHandlerFactory: (Counter) -> Increment) : Increment {
              
                private val incrementHandler: Increment = incrementHandlerFactory(this)
              
                }
              
                override fun increment() {
              
                incrementHandler.increment()
              
                }
              
___

Create a free account to access the full topic