Complete the code to declare an enum class named Color.
enum class [1] { RED, GREEN, BLUE }
The enum class must be named Color exactly as specified.
Complete the code to add a property 'rgb' to each enum constant.
enum class Color(val [1]: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) }
The property name is rgb to represent the color code.
Fix the error in the method that returns the color name in lowercase.
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF); fun colorName() = name.[1]() }
toLowerCase() which is deprecated in Kotlin.In Kotlin, the correct method to convert a string to lowercase is lowercase().
Fill both blanks to add a method that returns the hex string of the rgb value.
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF); fun hex() = "#$[1].format(\"%06X\", [2] )" }
Int.format which does not exist.this.rgb instead of just rgb.The String.format method is used to format the rgb integer as a hex string.
So String.format and rgb are needed.
Fill all three blanks to create a companion object with a method that finds a Color by its rgb value.
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF); companion object { fun fromRgb(value: Int): Color? = values().[1] { it.[2] == [3] } } }
find which is not a Kotlin standard function.The method firstOrNull finds the first enum constant matching the condition or returns null.
The lambda compares it.rgb with the input value.