All examples can be found in the GitHub repository of this very course, at https://github.com/KULeuven-Diepenbeek/appdev-course/tree/main/examples
We’ll be using IntelliJ to create simple Kotlin projects, so leave Android Studio closed for the moment. While creating a new project, choose
Wait for the initial Gradle download/build to finish. Create your Kotlin class/source files in src/main/kotlin
. Do not forget creating the correct package structure. See the examples in the above GitHub link.
The Kotlin DSL (Domain Specific Language) provides an alternative syntax to the traditional Groovy DSL in Gradle buidlfiles with superious IDE support. This means that every statement in your build script should be valid Kotlin syntax. For more information, inspect to the Software Engineering Skills: Kotlin Gradle files course information page.
Remember the backpack exercise from INF1 last year? The one in BlueJ where you had to add different items into your backpack, of which each has its own weight, and calculate the total weight of the pack?
Let’s try to implement a simple version of that in Kotlin.
Stuff
class. It has an immutable weight
property.Backpack
class. It should contain a collection where the Stuff
instances can be put in: create an add()
function, as well as a weight
getter that calculates ad-hoc.main
function. Fool around by adding stuff into the backpack.Things of note to take into account:
weight
defined? As a property or a function?foreach
style did you employ?null
here? What if the backpack is empty?Remember the taxi exercise from INF1 last year? The one in BlueJ that teaches you about object composition?
Model a taxi company. The company can hire drivers, buy cars, and ultimately, drive clients from an address in point A to point B, to hopefully make some money.
This is simple enough in Java and teaches you how to:
TaxiFirm
holds a reference to one or more Car
s and Driver
s)First, take a look at the java solution. Then, try to do it yourself in the Kotlin JVM. If all else fails, peek at the provided solution.
Remember the basic JavaFX exercises from INF1, where we created a smiley or person, virtually walking around on the screen? Those labs acted as a first acquaintance with the JavaFX and Movel-View-Controller concepts. The assignment was as follows:
Create a custom
Region
and render a simple person consisting out of multiple rectangles. Create buttons up/down/left/right and implement the click events such that the person moves around on the screen in the correct direction.
This teaches you how to:
AnchorPane
and put stuff onto it using SceneBuilderIf that doesn’t refresh your memory, take a look at the java solution. Then, try to do it yourself in the Kotlin JVM. If all else fails, peek at the provided solution.
Since this is a JavaFX Gradle application, it cannot be started by pressing the play button: the JavaFX module options should be passed along. We rely on the org.openjfx.javafxplugin
for this: see this build.gradle.kts
file you can copy over. To run the application, execute the Gradle task application – run.
When you’re done, compare your solution with the one provided. The following constructs were modified from the Java version:
getChilden().add(x);
, you just use children.add(x)
.init {}
block is needed in the view if you want to call a method in the primary constructor.MyClass::class.java.getResources()
. See MainApp.kt
.125
lines in total), compared to the java implementation (157
lines).class Main : Application()
. Notice the brackets. Also note extends
is replaced by a simple semicolon.There are a lot of things that can go wrong when calling Java from Kotlin. For example, some Kotlin-specific keywords, such as when
, can be simply functions in Java. To call these, you need to use backticks. Consult the Kotlin-Java interop guide if you encounter a weird error while calling a native Java method.
More small and large examples can be found in the GitHub repository of this very course, at https://github.com/KULeuven-Diepenbeek/appdev-course/tree/main/examples/kotlin
On kotlinlang.org
, examples that explore every aspect of the Kotlin language are neatly summarized using their online Playground: https://play.kotlinlang.org/byExample/overview. We strongly recommend you to check these out: they are very short, do not require anything to install, and touch upon every basic but important concept you’ll need to master.
There are also Koans available https://play.kotlinlang.org/koans/overview that let you get familiar with the Kotlin syntax by training again and again until the syntax has been well-imprinted into your brain.