0
0
Kotlinprogramming~10 mins

Enum with properties and methods in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an enum class named Color.

Kotlin
enum class [1] {
    RED, GREEN, BLUE
}
Drag options to blanks, or click blank then click option'
Acolor
BPaint
CColors
DColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for enum classes.
Using plural form when singular is expected.
2fill in blank
medium

Complete the code to add a property 'rgb' to each enum constant.

Kotlin
enum class Color(val [1]: Int) {
    RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF)
}
Drag options to blanks, or click blank then click option'
Avalue
Brgb
Ccode
DcolorCode
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'code' or 'value' instead of 'rgb'.
Using camelCase incorrectly.
3fill in blank
hard

Fix the error in the method that returns the color name in lowercase.

Kotlin
enum class Color(val rgb: Int) {
    RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF);

    fun colorName() = name.[1]()
}
Drag options to blanks, or click blank then click option'
Alowercase
BtoLowerCase
CtoUpperCase
Duppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java method toLowerCase() which is deprecated in Kotlin.
Using uppercase methods instead of lowercase.
4fill in blank
hard

Fill both blanks to add a method that returns the hex string of the rgb value.

Kotlin
enum class Color(val rgb: Int) {
    RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF);

    fun hex() = "#$[1].format(\"%06X\", [2] )"
}
Drag options to blanks, or click blank then click option'
AString
Brgb
CInt
Dthis.rgb
Attempts:
3 left
💡 Hint
Common Mistakes
Using Int.format which does not exist.
Using this.rgb instead of just rgb.
5fill in blank
hard

Fill all three blanks to create a companion object with a method that finds a Color by its rgb value.

Kotlin
enum class Color(val rgb: Int) {
    RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF);

    companion object {
        fun fromRgb(value: Int): Color? = values().[1] { it.[2] == [3] }
    }
}
Drag options to blanks, or click blank then click option'
Afind
Brgb
Cvalue
DfirstOrNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using find which is not a Kotlin standard function.
Mixing up parameter names inside the lambda.