I must say, I am happy with the great feedback I received for the book Kotlin Essentials. In the end, it is a result of years of practice in teaching Kotlin. However, there was one request I also often received: Readers commented that this book would benefit from practical exercises after each chapter. They argued that such exercises would help them to better understand and practice the presented concepts. I agree with this argument, and I am happy to announce that the book Kotlin Essentials now has exercises after each chapter.
About exercises
I did my best for each exercise to base only on what was already covered in the book, and to be and appropriate level for reader. Some exercises might seem easy, like the one after the section about basic values and operations, that ask you to predict what will be printed as a result of specific operations.
Others are very practical, like the first exercise that makes reader follow a set of steps to create and run the first program in Kotlin. Finally, some exercises might be demanding, like the exercise that ends the section about generics, and asks to implement Stack.
The majority of exercises have starting code, sample usages, and unit tests. I located them in the project kotlin-exercises, that can be cloned and used as a starting point for each exercise. Solutions are presented on the last pages of the book.
If you are teaching Kotlin at a public school or a university, feel free to use my exercises on your course.
Sample exercise: Pretty time display
Implement the secondsToPrettyTime function that takes an integer number of seconds and returns a string representation of the time in the following format: "X h Y min Z sec", where X, Y, and Z are the number of hours, minutes, and seconds respectively. If a value is zero, it should be omitted from the result. If the input is negative, return "Invalid input".
fun secondsToPrettyTime(seconds: Int): String {
return ""
}
Example usage:
fun main() {
println(secondsToPrettyTime(-1)) // Invalid input
println(secondsToPrettyTime(0)) //
println(secondsToPrettyTime(45)) // 45 sec
println(secondsToPrettyTime(60)) // 1 min
println(secondsToPrettyTime(150)) // 2 min 30 sec
println(secondsToPrettyTime(1410)) // 23 min 30 sec
println(secondsToPrettyTime(60 * 60)) // 1 h
println(secondsToPrettyTime(3665)) // 1 h 1 min 5 sec
}
Starting code, unit tests and example usage can be found in the kotlin-exercises project on GitHub in the file essentials/conditions/PrettyTime.kt. You can clone this project and solve this exercise locally.
Hint: You can use trim function on a string to remove leading and trailing whitespace characters.
Ending
I hope those exercises will help readers' practice present a topic, and as a result, understand it on a deeper level. I believe this is a big step for making this book better.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.