google-developer-training / kotlin-bootcamp

Report issues with the Kotlin Bootcamp for Programmers codelab
7 stars 2 forks source link

Kotlin Bootcamp issue: Random should be used instead of Random(), Lesson 3, Step 4 #105

Closed youseftcg closed 3 years ago

youseftcg commented 3 years ago

Describe the problem An instance of a class is used while it's an abstract class,Random().nextInt() is used, while Random.nextInt() is the one that should be used.

In which step of the codelab can this issue be found? Step 3 in the second snippet of code:

fun randomDay() : String {
    val week = arrayOf ("Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday")
    return week[Random().nextInt(week.size)]    // here is the problem, I believe Random.nextInt() should be used, otherwise an 
                                              // error of "Cannot create an instance of an abstract class" is thrown

}

codelab: kotlin-bootcamp

youseftcg commented 3 years ago

It turned out that it's not a mistake, but rather, Random() in the codelab refers to the Radom class in Java, which has a normal consturctor, hence using Random() is ok if the Random Java class is imported, however, if the user is using the Random Kotlin Class, an error will be thrown "Cannot create an instance of an abstract class"