CS0011 Module 3

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/42

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:24 PM on 10/6/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

43 Terms

1
New cards

Modern, trending programming language that was released in 2016 by JetBrains

Kotlin

2
New cards

Kotlin is a modern, trending programming language that was released in __ by JetBrains

2016

3
New cards

Kotlin is a modern, trending programming language that was released in 2016 by __

JetBrains

4
New cards

Compatible with Java

Kotlin

5
New cards

Works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)

Kotlin

6
New cards

Concise and safe

Kotlin

7
New cards

Easy to learn, especially if you already know Java

Kotlin

8
New cards

Free to use

Kotlin

9
New cards

Big community/support

Kotlin

10
New cards
  • Mobile applications (specially Android apps)

  • Web development

  • Server side applications

  • Data science

  • And much, much more!


Uses of Kotlin

11
New cards
<p>The __ keyword is used to declare a function</p>

The __ keyword is used to declare a function

fun

12
New cards
<p>The __ function is needed to execute a program</p>

The __ function is needed to execute a program

main()

13
New cards
<p>In Kotlin, code statements do not have to end with a __ (_)</p>

In Kotlin, code statements do not have to end with a __ (_)

semicolon (;)

14
New cards
<pre><code class="language-Kotlin">//</code></pre><p></p>
//


Single line comments

15
New cards
<pre><code class="language-Kotlin">/* … */</code></pre><p></p>
/* … */


Multi-line comments

16
New cards
<p>The __ keyword can create a variable whose reference will be&nbsp;<strong>immutable&nbsp;</strong>(which cannot be modified after its initialization)</p>

The __ keyword can create a variable whose reference will be immutable (which cannot be modified after its initialization)

val

17
New cards
<p>The __&nbsp;keyword lets you create a variable whose reference will be&nbsp;<strong>mutable&nbsp;</strong>(which can be modified after its initialization)</p>

The __ keyword lets you create a variable whose reference will be mutable (which can be modified after its initialization)

var

18
New cards
<p>Kotlin does&nbsp;<strong>not require&nbsp;</strong>you to mention the __ __ __ __ when declaring it (thanks to type inference)</p>

Kotlin does not require you to mention the __ __ __ __ when declaring it (thanks to type inference)

type of a variable

19
New cards
<pre><code class="language-Kotlin">val time = 20
val greeting = if (time &lt; 18) {
    "Good day."
} else {
    "Good evening."
}
println(greeting)</code></pre><p></p>
val time = 20
val greeting = if (time < 18) {
    "Good day."
} else {
    "Good evening."
}
println(greeting)


  • if

  • if – else

  • if – else if

  • if – else expression


20
New cards
<pre><code class="language-Kotlin">val time = 20
val greeting = if (time &lt; 18)
    "Good day." else "Good evening."</code></pre><p></p>
val time = 20
val greeting = if (time < 18)
    "Good day." else "Good evening."


  • if

  • if – else

  • if – else if

  • if – else expression


21
New cards
<pre><code class="language-Kotlin">val day = 4 

val result = when (day) {
    1 -&gt; "Monday"
    2 -&gt; "Tuesday"
    3 -&gt; "Wednesday"
    4 -&gt; "Thursday"
    5 -&gt; "Friday"
    6 -&gt; "Saturday"
    7 -&gt; "Sunday"
    else -&gt; "Invalid day."
}
println(result)

// Outputs "Thursday" (day 4)</code></pre><p></p>
val day = 4 

val result = when (day) {
    1 -> "Monday"
    2 -> "Tuesday"
    3 -> "Wednesday"
    4 -> "Thursday"
    5 -> "Friday"
    6 -> "Saturday"
    7 -> "Sunday"
    else -> "Invalid day."
}
println(result)

// Outputs "Thursday" (day 4)


when expression

22
New cards
<pre><code class="language-Kotlin">val nums = arrayOf(1, 5, 10, 15, 20)
for (x in nums) {
    println(x)
}</code></pre><p></p>
val nums = arrayOf(1, 5, 10, 15, 20)
for (x in nums) {
    println(x)
}


// for loop using arrays

  • while

  • do – while

  • for – loop


23
New cards
<pre><code class="language-Kotlin">for (nums in 5..15) {
    println(nums)
}</code></pre><p></p>
for (nums in 5..15) {
    println(nums)
}


// for loop using range

  • while

  • do – while

  • for – loop


24
New cards
<pre><code class="language-Kotlin">class Car {
    var brand = ""
    var model = ""
    var year = 0
}</code></pre><p></p>
class Car {
    var brand = ""
    var model = ""
    var year = 0
}


class declaration (OOP)

25
New cards
<pre><code class="language-Kotlin">// Create a c1 object of the Car class
val c1 = Car() 

// Access the properties and add some values to it
c1.brand = "Ford"
c1.model = "Mustang"
c1.year  = 1969

println(c1.brand)    // Outputs Ford
println(c1.model)    // Outputs Mustang
println(c1.year)     // Outputs 1969</code></pre><p></p>
// Create a c1 object of the Car class
val c1 = Car() 

// Access the properties and add some values to it
c1.brand = "Ford"
c1.model = "Mustang"
c1.year  = 1969

println(c1.brand)    // Outputs Ford
println(c1.model)    // Outputs Mustang
println(c1.year)     // Outputs 1969


class instantiation (OOP)

26
New cards
<pre><code class="language-Kotlin">class Car(var brand: String, var model: String, var year: Int)

fun main() {
    val c1 = Car("Ford", "Mustang", 1969)
}</code></pre><p></p>
class Car(var brand: String, var model: String, var year: Int)

fun main() {
    val c1 = Car("Ford", "Mustang", 1969)
}


using constructor (OOP)

27
New cards
<pre><code class="language-Kotlin">class Car(var brand: String, var model: String, var year: Int) {
  // Class function
  fun drive() {
    println("Wrooom!")
  }

  // Class function with parameters
  fun speed(maxSpeed: Int) {
    println("Max speed is: " + maxSpeed)
  }
}

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  
  // Call the functions
  c1.drive()
  c1.speed(200)
}</code></pre><p></p>
class Car(var brand: String, var model: String, var year: Int) {
  // Class function
  fun drive() {
    println("Wrooom!")
  }

  // Class function with parameters
  fun speed(maxSpeed: Int) {
    println("Max speed is: " + maxSpeed)
  }
}

fun main() {
  val c1 = Car("Ford", "Mustang", 1969)
  
  // Call the functions
  c1.drive()
  c1.speed(200)
}


class & functions (OOP)

28
New cards
<pre><code class="language-Kotlin">class Person (val _name: String) {
    // Member Variables
    var name: String

    // Initializer Blocks
    init {
        println("This is first init block")
    }

    init {
        println("This is second init block")
    }

    init {
        println("This is third init block")
    }

    init {
        this.name = _name
        println("Name = $name")
    }
}

fun main(args: Array&lt;String&gt;) {
    val person = Person("Abe")
}</code></pre><p></p>
class Person (val _name: String) {
    // Member Variables
    var name: String

    // Initializer Blocks
    init {
        println("This is first init block")
    }

    init {
        println("This is second init block")
    }

    init {
        println("This is third init block")
    }

    init {
        this.name = _name
        println("Name = $name")
    }
}

fun main(args: Array<String>) {
    val person = Person("Abe")
}


Using init blocks (OOP)

<p>Using <em>init </em>blocks (OOP)</p>
29
New cards
term image

Default value in primary constructor (OOP)

30
New cards
term image

Using the secondary constructor (OOP)

31
New cards
term image

inheritance (OOP)

32
New cards

Kotlin vs. Java:

Features: Extension Functions

Kotlin: It is already available in Kotlin

Java: In java, we need to create class

33
New cards

Kotlin vs. Java:

Features: Null Safety

Kotlin: It is available in Kotlin

Java: It is not available in Java

34
New cards

Kotlin vs. Java:

Features: Static Members

Kotlin: Kotlin doesn’t have a static member for a class

Java: It is available in Java

35
New cards

Kotlin vs. Java:

Features: String Templates

Kotlin: Yes, there are two types of string literals in Kotlin

Java: It is available in Java too but it doesn’t support expression like Kotlin

36
New cards

Kotlin vs. Java:

Features: Wildcard Types

Kotlin: It is not available in Kotlin

Java: Available in Java

37
New cards

Kotlin vs. Java:

Features: Smartcasts

Kotlin: Available in Kotlin

Java: Not Available in Java

38
New cards

Kotlin vs. Java:

Features: No Checked Exception

Kotlin: Kotlin removed exceptions entirely

Java: It is problematic in Java

39
New cards

Kotlin vs. Java:

Features: Operator Overloading

Kotlin: Kotlin allows users to provide a way to invoke functions

Java: Operators are tied to particular Java Types

40
New cards

Kotlin vs. Java:

Features: Constructors

Kotlin: It has primary constructor and secondary constructor

Java: Constructors can be used to take parameters to initialize attributes

41
New cards

Kotlin vs. Java:

Features: Type System

Kotlin: It gives nullability support, type inference, and universal guards

Java: There are other kinds of reference types related to the basic concept of class

42
New cards
<p></p>


Kotlin in Android

43
New cards
term image

Kotlin in Android (Java)