Practice: Kotlin BasicsΒΆ
String templates. This program informs users about the upcoming promotional sale on a particular item. It has a string template, which relies on the
discountPercentagevariable for the percent discount and theitemvariable for the item on sale. However, there are compilation errors in the code.fun main() { val discountPercentage: Int = 0 val offer: String = "" val item = "Google Chromecast" discountPercentage = 20 offer = "Sale - Up to $discountPercentage% discount on $item! Hurry up!" println(offer) }
Can you figure out the root cause of the errors and fix them?
Can you determine the output of this program before you run the code in Kotlin Playground?
After you fix the errors, the program should compile without errors and print this output:
Sale - Up to 20% discount on Google Chromecast! Hurry up!
Default parameters. Gmail has a feature that sends a notification to the user whenever a login attempt is made on a new device. In this exercise, you write a program that displays a message to users with this message template:
There's a new sign-in request on $operatingSystem for your Google Account $emailId.
You need to implement a function that accepts an
operatingSystemparameter and anemailIdparameter, constructs a message in the given format, and returns the message.For example, if the function was called with
"Chrome OS"for theoperatingSystemand"sample@gmail.com"for theemailId, it should return this string:There's a new sign-in request on Chrome OS for your Google Account sample@gmail.com.
Implement the
displayAlertMessage()function in this program so that it prints the output displayed. If the operating system is unknown, the function should default toUnknown OS.fun main() { val firstUserEmailId = "user_one@gmail.com" // The following line of code assumes that you named your parameter as emailId. // If you named it differently, feel free to update the name. println(displayAlertMessage(emailId = firstUserEmailId)) println() val secondUserOperatingSystem = "Windows" val secondUserEmailId = "user_two@gmail.com" println(displayAlertMessage(secondUserOperatingSystem, secondUserEmailId)) println() val thirdUserOperatingSystem = "Mac OS" val thirdUserEmailId = "user_three@gmail.com" println(displayAlertMessage(thirdUserOperatingSystem, thirdUserEmailId)) println() } // Define your displayAlertMessage() below this line.