/** * Kotlin Fundamentals — Practical Exercises * Mobile Application Development | SUZA | Semester II 2025/2026 * * Run each exercise from main(). Uncomment the line you want to test. * You can run individual files at https://play.kotlinlang.org */ fun main() { // ex01_variables() // ex02_gradeCalculator() // ex03_fizzBuzz() // ex04_listOperations() // ex05_studentClass() // ex06_nullSafety() // ex07_dataClassAndCopy() // ex08_higherOrderFunctions() // ex09_whenExpression() // ex10_extensionFunction() } // 1. Variables — declare, print, and swap two values without a temp variable. fun ex01_variables() { var a = 5 var b = 10 // TODO: swap a and b using one line (hint: destructuring) a = b.also { b = a } println("a=$a, b=$b") } // 2. Grade Calculator using "when" fun grade(score: Int): String = when { score >= 80 -> "A" score >= 70 -> "B" score >= 60 -> "C" score >= 50 -> "D" else -> "F" } fun ex02_gradeCalculator() { listOf(95, 72, 58, 33).forEach { println("$it -> ${grade(it)}") } } // 3. FizzBuzz 1..30 fun ex03_fizzBuzz() { for (i in 1..30) { println(when { i % 15 == 0 -> "FizzBuzz" i % 3 == 0 -> "Fizz" i % 5 == 0 -> "Buzz" else -> i.toString() }) } } // 4. List Operations fun ex04_listOperations() { val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) println("Sum = ${nums.sum()}") println("Evens = ${nums.filter { it % 2 == 0 }}") println("Doubled = ${nums.map { it * 2 }}") println("Sum of squares = ${nums.sumOf { it * it }}") println("Max = ${nums.maxOrNull()}") } // 5. Student class with method class Student(val name: String, var gpa: Double) { fun standing(): String = when { gpa >= 3.5 -> "Dean's List" gpa >= 2.0 -> "Good Standing" else -> "Probation" } override fun toString() = "$name (GPA=$gpa, ${standing()})" } fun ex05_studentClass() { val students = listOf(Student("Amina", 3.8), Student("Juma", 2.1), Student("Zahra", 1.7)) students.forEach(::println) } // 6. Null Safety fun ex06_nullSafety() { val name: String? = null println("Length: ${name?.length ?: 0}") // Elvis operator val upper = name?.uppercase() ?: "UNKNOWN" // default println("Upper: $upper") // val boom = name!!.length // would throw NPE — avoid } // 7. Data class, copy(), destructuring data class Book(val title: String, val author: String, val pages: Int) fun ex07_dataClassAndCopy() { val b1 = Book("Clean Code", "Robert Martin", 464) val b2 = b1.copy(pages = 500) val (t, a, p) = b2 println("$t by $a, $p pages") println("b1 == b2? ${b1 == b2}") // data class equality } // 8. Higher-order functions fun transform(list: List, f: (T) -> R): List = list.map(f) fun ex08_higherOrderFunctions() { val words = listOf("suza", "mobile", "compose") println(transform(words) { it.uppercase() }) println(transform(words, String::length)) } // 9. "when" with ranges, types, predicates fun describe(obj: Any): String = when (obj) { is Int -> "Int $obj" is String -> "String of length ${obj.length}" in 1.0..10.0 -> "Double in 1..10" null -> "null" else -> "Something else" } fun ex09_whenExpression() { listOf(5, "hello", 3.14, null, listOf(1)).forEach { println(describe(it ?: "null")) } } // 10. Extension function fun String.reverseWords(): String = split(" ").reversed().joinToString(" ") fun ex10_extensionFunction() { println("hello world from suza".reverseWords()) }