Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming features

Standard delegates

Student

Report a typo

Write a class Student that has a name property of type String and a notNull property id of type Int. The value of the id property must be initialized before it is accessed, and if an attempt is made to access it before it is initialized, an exception should be thrown.

Sample Input 1:

10

Sample Output 1:

Property id should be initialized before get.
10
Write a program in Kotlin
import kotlin.properties.Delegates

class Student {

// Write your code here

}

// Do not change the code below
fun main() {
val student = Student()
val id = readln().toInt()
try {
println(student.id)
} catch (e: Exception) {
println(e.message)
}
student.id = id
println(student.id)
}
___

Create a free account to access the full topic