0
0
Kotlinprogramming~15 mins

Enum class declaration in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum class declaration
📖 Scenario: Imagine you are creating a simple app that needs to handle different types of weather conditions.
🎯 Goal: You will create an enum class in Kotlin to represent different weather types.
📋 What You'll Learn
Create an enum class named Weather with specific weather types
Add a variable to hold a current weather value
Use the enum class in a simple function
Print the current weather
💡 Why This Matters
🌍 Real World
Enum classes help represent fixed sets of related constants, like days, states, or categories, making code clearer and safer.
💼 Career
Understanding enum classes is important for Kotlin developers working on Android apps or backend services where fixed sets of options are common.
Progress0 / 4 steps
1
Create the enum class
Create an enum class called Weather with these exact entries: SUNNY, CLOUDY, RAINY, SNOWY.
Kotlin
Need a hint?

Use the enum class keyword followed by the name Weather. List the weather types separated by commas inside curly braces.

2
Create a variable for current weather
Create a variable called currentWeather of type Weather and set it to Weather.SUNNY.
Kotlin
Need a hint?

Use val currentWeather: Weather = Weather.SUNNY to create the variable.

3
Write a function to describe the weather
Write a function called describeWeather that takes a parameter weather of type Weather and returns a String. Use a when expression on weather to return these exact strings: "It's sunny today!" for Weather.SUNNY, "It's cloudy today." for Weather.CLOUDY, "It's raining." for Weather.RAINY, and "Snow is falling." for Weather.SNOWY.
Kotlin
Need a hint?

Use a when expression to check the weather parameter and return the matching string.

4
Print the current weather description
Print the result of calling describeWeather with currentWeather.
Kotlin
Need a hint?

Use println(describeWeather(currentWeather)) inside a main function to print the weather description.