Practice Time: whatShouldIDoToday()
Practice Time
Create a program that suggests an activity based on various parameters.
- Start in a new file with a
main
function. - From
main()
, create a function,whatShouldIDoToday()
. - Let the function have three parameters.
mood
: a required string parameterweather
: a string parameter that defaults to "sunny"temperature
: an Integer parameter that defaults to 24 (Celsius).
- Use a
when
construct to return some activities based on combinations of conditions. For example:
mood == "happy" && weather == "Sunny" -> "go for a walk" else -> "Stay home and read."
- Copy/paste your finished function into REPL, and call it with combinations of arguments. For example:
whatShouldIDoToday("sad") > Stay home and read.
// File: Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/15/2023
// Email: nolvertou@gmail.com
// Description: Create a program that suggests an activity based on various parameters.
fun main() {
println("Hello Kotlin, What should I do today?")
print(whatShouldIDoToday("sad"))
}
/**
* whatShouldIDoToday function suggests an activity based on various parameters:
* @param mood: a required string parameter
* @param weather: a string parameter that defaults to "sunny"
* @param temperature: an Integer parameter that defaults to 24 (Celsius)
*/
fun whatShouldIDoToday(mood: String,
weather: String = "sunny",
temperature: Int = 24): String{
return when{
mood == "happy" && weather == "Sunny" -> "go for a walk"
else -> "Stay home and read."
}
}
Output
Hello Kotlin, What should I do today?
Stay home and read.
Process finished with exit code 0
Comments
Post a Comment