Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming features

Standard delegates

Stop the count!

Report a typo

Write a class Counter that has a maxCount property and a vetoable property count that ensures that the value of the count property never exceeds the value of the maxCount property. If an attempt is made to set the count property to a value greater than the value of the maxCount property, the new value should be rejected.

Both count and maxCount are of type Int.

Sample Input 1:

80

Sample Output 1:

78 79 80 80 80 
Write a program in Kotlin
import kotlin.properties.Delegates

class Counter {
// Do not change the declaration of the property: maxCount
var maxCount: Int = 0

// Declare here the delegated property: count
}

// Do not change the code below
fun main() {
val counter = Counter()
val maxCount = readln().toInt()
counter.maxCount = maxCount

for (i in maxCount - 2..maxCount + 2) {
counter.count = i
print("${counter.count} ")
}
}
___

Create a free account to access the full topic