Kotlin Essentials now with exercises
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".
Example usage:
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.