Chapter: 1
Introduction
Welcome to Kotlin!
Kotlin is a modern programming language that is concise, safe, pragmatic, and focused on interoperability with Java code.
It can be used almost everywhere Java is used today: for server-side development, Android apps, and much more.
Recently Google announced that Android development will be increasingly Kotlin-first and that many top apps have already migrated to Kotlin.
Welcome to Kotlin!
Kotlin can be used to develop Android apps.
Hello World!
Let's start by making a program to output "Hello World" to the screen:
fun main(args : Array<String>) {
println("Hello, World!")
}
println("Hello, World!")
}
KT
The println() function is used to output the text that is provided between its parentheses.
We will learn about functions in the coming modules. For now, just remember that the println() function is used to output text.
Hello World!
Drag and drop from the options below to output "Kotlin is fun".
Chapter: 2
Data-types
Data Types
Each value in Kotlin must have a type.
Some of the most used built-in data types are:
Int indicates an integer (whole number), such as 42.
Double and Float are used to store decimal numbers, such as 12.4.
Char represents a character, such as 'z'.
Boolean has two possible values, either true or false.
We will be using data types to assign and manipulate values in the next lesson.
Data Types
Drag and drop the corresponding data types to the given values:
Data Types
The String type is used to represent text, such as "Hello World".
The text for a String is enclosed in double quotes.
The \n character can be used in a string to create a line break, for example:
println("A \n B \n C")
KT
The code above will output each letter on a new line:
A
B
C
A
B
C
Data Types
How many lines will the following code output? println("Hey\n there")
Chapter: 3
Variables
Variables
Variables are used to store data.
Each variable has a type that defines the type of the data that it holds.
Variables are declared using the var keyword:
var num: Int = 42
KT
Now we can use num in our code, for example output its value using println():
println(num)
KT
You can reassign a variable in your code, for example: num = 8
Variables
Fill in the blanks to declare a variable called name and assign the value "James" to it.
Variables
You can also declare variables using the val keyword:
val course: String = "Kotlin"
println(course)
println(course)
KT
If a variable must be changeable, then declare it as a var. Otherwise, use val.
Variables
Fill in the blanks to declare a variable named "color" and assign the value 42 to it. The variable should not be changeable.
Type Inference
Kotlin supports a useful feature called type inference, which allows it to guess the type of the variable from the value it is assigned to.
For example, when you assign the value "James" to a variable, Kotlin infers that the value is a String and automatically sets the type of the variable.
This allows us to skip the type definition in variable declarations:
val name = "James"
var num = 42
var num = 42
KT
In the code above, name is a String and num is an Int.
Type Inference
What is the type of the "test" variable? val test = "88"
Chapter: 4
Operators
Arithmetic Operators
Kotlin supports all common arithmetic operators:
+ for addition
- for subtraction
* for multiplication
/ for division
% for modulus
You can use them for variables and values in your code:
var num1 = 8
var num2 = 34
println(num1 + num2)
println(num2 - num1)
println(num1 * num2)
println(num2 / num1)
println(num2 % num1)
var num2 = 34
println(num1 + num2)
println(num2 - num1)
println(num1 * num2)
println(num2 / num1)
println(num2 % num1)
KT
The plus operator + can also be used to add together two strings, for example "Hey "+"there" results in "Hey there". This process is called concatenation.
Arithmetic Operators
What is the output of this code? var num1 = 3 var num2 = 6 var x = num2 / num1 println(num2 / x)
Assignment Operators
The assignment operator = is used to assign a value to a variable.
Kotlin also supports arithmetic assignment operators, for example a+=b is equivalent to a=a+b.
The same applies to the other operators. For example:
var num = 4
num *= 5
println(num)
num *= 5
println(num)
KT
var num = 8
num++
println(num)
num++
println(num)
KT
Both, increment and decrement operators have prefix (before the name of the variable) and postfix (after the name of the variable) forms. The prefix form increments the variable and then uses it
in the assignment statement. The postfix form uses the value of the variable first, before incrementing it.
in the assignment statement. The postfix form uses the value of the variable first, before incrementing it.
Assignment Operators
Fill in the blanks to calculate the sum of the two variables and output it.
Comparison Operators
The comparison operators compare two values and return a boolean: true or false.
For example, 10>5 will return true.
Here are the comparison operators:
> greater than
< less than
>= greater than or equals to
<= less than or equals to
== is equal to
!= is not equal
For example, we can check and output whether the age variable is greater than or equal to 16:
var age = 18
println(age >= 16)
println(age >= 16)
KT
The comparison operators are used in conditional statements, which will be described in a later lesson.
Comparison Operators
Fill in the required operator to output whether the variables "a" and "b" are equal:
Chapter: 5
Comments
Comments
Comments are explanatory statements used to describe your code.
A single line comment starts with //.
For example:
// my first Kotlin program
fun main(args : Array<String>) {
// declaring a name variable
var name = "Amy"
println(name)
}
fun main(args : Array<String>) {
// declaring a name variable
var name = "Amy"
println(name)
}
KT
Everything after // is considered as a comment and is ignored by the program.
Comments
Fill in the blank to make a valid comment.
Comments
If you need a longer comment that spans multiple lines, you can use multi-line comments. Everything between /* and */ is considered a comment.
For example:
fun main(args : Array<String>) {
/* this is a multi-line comment.
The program declares a string and outputs it */
var name = "Amy"
println(name)
}
/* this is a multi-line comment.
The program declares a string and outputs it */
var name = "Amy"
println(name)
}
KT
Comments make it easier to read and understand code.
Comments
Rearrange the code to create a valid multi-line comment containing the text "Kotlin is fun".
Chapter: 6
Input
Input
You can take values as input from the user using the readLine() function.
For example, we can ask the user to enter their age and store it in a variable:
var age = readLine()
println("You entered " + age)
println("You entered " + age)
KT
The readLine() function prompts the user for input and returns the value that the user inputs as a string.
Input
Fill in the blanks to take the name of the user as input and output it:
Input
readLine() returns the input as a string, even if the user enters a number.
So how do we take numbers as input?
We need to convert the input to the corresponding type. For example, to convert to Int we need to use toInt():
var a = readLine()!!.toInt()
var b = readLine()!!.toInt()
println(a+b)
var b = readLine()!!.toInt()
println(a+b)
KT
Note the !! after readLine(): this is called the not-null assertion operator, and it defines that the expression on its left is not-null, which means that it has to have a valid value, which is not blank (or null).
It is required for toInt() to work.
You can use toDouble() to convert a string to Double.
Input
Drag and drop from the options below to take input and convert it to a Double:
Chapter: 7
Module 1 Quiz
Fill in the blanks to take two integers as input and output their product:
Which of the following options declares a variable that cannot change its value?
What is the output of this code? var x = 4 x *= 2 x -= 3 x++ println(x)
What type is the result of comparing two values?
Code Project
Water Consumption
Water Consumption
Each day a family consumes 15 liters of water.
Given the number of years as input, you need to calculate and output the amount of water consumed in that period.
Sample Input
5
Sample Output
27375
The water consumption in 5 years would be: 5*365*15 = 27375
Note: Comment down your answer after solving the question.
Use 365 as the number of days in a year.


0 Comments