From 850604191ebb82d50638f426b167b556313690a9 Mon Sep 17 00:00:00 2001 From: Eko Kurniawan Khannedy Date: Sat, 11 Apr 2020 23:21:03 +0700 Subject: [PATCH] push project belajar kotlin dasar --- .gitignore | 1 + .idea/.gitignore | 8 ++++ .idea/codeStyles/Project.xml | 10 ++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/kotlinc.xml | 10 ++++ .idea/libraries/KotlinJavaRuntime.xml | 19 ++++++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ belajar-kotlin-dasar.iml | 12 +++++ src/AnonymousFunction.kt | 23 ++++++++++ src/Array.kt | 21 +++++++++ src/Boolean.kt | 7 +++ src/BreakContinue.kt | 23 ++++++++++ src/Character.kt | 9 ++++ src/Closure.kt | 28 ++++++++++++ src/DoWhileLoop.kt | 10 ++++ src/ExtensionFunction.kt | 13 ++++++ src/ForLoop.kt | 17 +++++++ src/Function.kt | 11 +++++ src/FunctionDefaultParameter.kt | 14 ++++++ src/FunctionInfixNotation.kt | 12 +++++ src/FunctionNamedArgument.kt | 16 +++++++ src/FunctionParameter.kt | 13 ++++++ src/FunctionReturnType.kt | 27 +++++++++++ src/FunctionScope.kt | 19 ++++++++ src/FunctionVarargsParameter.kt | 16 +++++++ src/HelloWorld.kt | 8 ++++ src/HigherOrderFunction.kt | 17 +++++++ src/IfExpression.kt | 15 ++++++ src/InlineFunction.kt | 12 +++++ src/Komentar.kt | 20 ++++++++ src/Label.kt | 35 ++++++++++++++ src/LambdaExpression.kt | 21 +++++++++ src/MainParameter.kt | 5 ++ src/Number.kt | 20 ++++++++ src/OperasiBoolean.kt | 16 +++++++ src/OperasiPerbandingan.kt | 16 +++++++ src/OperatorMatematika.kt | 33 ++++++++++++++ src/Package.kt | 8 ++++ src/Range.kt | 12 +++++ src/RecursiveFunction.kt | 19 ++++++++ src/ReturnIfWhen.kt | 19 ++++++++ src/SingleExpressionFunction.kt | 8 ++++ src/String.kt | 22 +++++++++ src/TailRecursiveFunction.kt | 19 ++++++++ src/UnitReturningFunction.kt | 7 +++ src/Variable.kt | 18 ++++++++ src/WhenExpression.kt | 53 ++++++++++++++++++++++ src/WhileLoop.kt | 12 +++++ src/com/company/belajar/util/StringUtil.kt | 9 ++++ 51 files changed, 788 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/libraries/KotlinJavaRuntime.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 belajar-kotlin-dasar.iml create mode 100644 src/AnonymousFunction.kt create mode 100644 src/Array.kt create mode 100644 src/Boolean.kt create mode 100644 src/BreakContinue.kt create mode 100644 src/Character.kt create mode 100644 src/Closure.kt create mode 100644 src/DoWhileLoop.kt create mode 100644 src/ExtensionFunction.kt create mode 100644 src/ForLoop.kt create mode 100644 src/Function.kt create mode 100644 src/FunctionDefaultParameter.kt create mode 100644 src/FunctionInfixNotation.kt create mode 100644 src/FunctionNamedArgument.kt create mode 100644 src/FunctionParameter.kt create mode 100644 src/FunctionReturnType.kt create mode 100644 src/FunctionScope.kt create mode 100644 src/FunctionVarargsParameter.kt create mode 100644 src/HelloWorld.kt create mode 100644 src/HigherOrderFunction.kt create mode 100644 src/IfExpression.kt create mode 100644 src/InlineFunction.kt create mode 100644 src/Komentar.kt create mode 100644 src/Label.kt create mode 100644 src/LambdaExpression.kt create mode 100644 src/MainParameter.kt create mode 100644 src/Number.kt create mode 100644 src/OperasiBoolean.kt create mode 100644 src/OperasiPerbandingan.kt create mode 100644 src/OperatorMatematika.kt create mode 100644 src/Package.kt create mode 100644 src/Range.kt create mode 100644 src/RecursiveFunction.kt create mode 100644 src/ReturnIfWhen.kt create mode 100644 src/SingleExpressionFunction.kt create mode 100644 src/String.kt create mode 100644 src/TailRecursiveFunction.kt create mode 100644 src/UnitReturningFunction.kt create mode 100644 src/Variable.kt create mode 100644 src/WhenExpression.kt create mode 100644 src/WhileLoop.kt create mode 100644 src/com/company/belajar/util/StringUtil.kt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c585e19 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..16533ee --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..1a7265d --- /dev/null +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..623b3eb --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a867696 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/belajar-kotlin-dasar.iml b/belajar-kotlin-dasar.iml new file mode 100644 index 0000000..245d342 --- /dev/null +++ b/belajar-kotlin-dasar.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AnonymousFunction.kt b/src/AnonymousFunction.kt new file mode 100644 index 0000000..a54ff45 --- /dev/null +++ b/src/AnonymousFunction.kt @@ -0,0 +1,23 @@ +fun main() { + + fun hello(name: String, transformer: (String) -> String): String { + val nameTransform = transformer(name) + return "Hello $nameTransform" + } + + val upper = fun(value: String): String { + if (value == "") { + return "UPS" + } else { + return value.toUpperCase() + } + } + + println(hello("Eko", upper)) + println(hello("", upper)) + + println(hello("Kurniawan", fun(value: String): String { + return value.toLowerCase() + })) + +} \ No newline at end of file diff --git a/src/Array.kt b/src/Array.kt new file mode 100644 index 0000000..845747a --- /dev/null +++ b/src/Array.kt @@ -0,0 +1,21 @@ +fun main() { + + val names: Array = arrayOf("Eko", "Joko", "Budi") + // names.set(0, "Rudi") + names[0] = "Rudi" + println(names[0]) + + val nilai: Array = arrayOf(10,9,8) + println(nilai[0]) + println(nilai[1]) + println(nilai[2]) + + val members: Array = arrayOfNulls(5) + members[0] = "Eko" + members[1] = "Eko" + members[2] = "Eko" + members[3] = null + members[4] = "Eko" + println(members.size) + +} \ No newline at end of file diff --git a/src/Boolean.kt b/src/Boolean.kt new file mode 100644 index 0000000..003965a --- /dev/null +++ b/src/Boolean.kt @@ -0,0 +1,7 @@ +fun main() { + var benar: Boolean = true + var salah: Boolean = false + + println(benar) + println(salah) +} \ No newline at end of file diff --git a/src/BreakContinue.kt b/src/BreakContinue.kt new file mode 100644 index 0000000..379673c --- /dev/null +++ b/src/BreakContinue.kt @@ -0,0 +1,23 @@ +fun main() { + + var i = 0 + + while (true) { + println("Hello ke $i") + i++ + + if (i > 1000) { + break + } + } + + for (i in 1..1000) { + + if (i % 2 == 0) { + continue + } + + println("Angka $i") + } + +} \ No newline at end of file diff --git a/src/Character.kt b/src/Character.kt new file mode 100644 index 0000000..b52b21d --- /dev/null +++ b/src/Character.kt @@ -0,0 +1,9 @@ +fun main() { + var e: Char = '3' + var k: Char = 'K' + var o: Char = '0' + + print(e) + print(k) + print(o) +} \ No newline at end of file diff --git a/src/Closure.kt b/src/Closure.kt new file mode 100644 index 0000000..06c8f7d --- /dev/null +++ b/src/Closure.kt @@ -0,0 +1,28 @@ +fun main() { + + var counter: Int = 0 + + val lambdaIncrement: () -> Unit = { + println("Lambda increment") + counter++ + } + + val anonymousIncremetn = fun() { + println("Anonymous Function increment") + counter++ + } + + fun functionIncrement(){ + println("Function increment") + counter++ + } + + lambdaIncrement() + anonymousIncremetn() + functionIncrement() + lambdaIncrement() + anonymousIncremetn() + functionIncrement() + + println(counter) +} \ No newline at end of file diff --git a/src/DoWhileLoop.kt b/src/DoWhileLoop.kt new file mode 100644 index 0000000..e11ce8b --- /dev/null +++ b/src/DoWhileLoop.kt @@ -0,0 +1,10 @@ +fun main() { + + var i = 0 + + do { + println("Perulangan ke $i") + i++ + } while (i < 10) + +} \ No newline at end of file diff --git a/src/ExtensionFunction.kt b/src/ExtensionFunction.kt new file mode 100644 index 0000000..287fccb --- /dev/null +++ b/src/ExtensionFunction.kt @@ -0,0 +1,13 @@ +fun String.hello(): String = "Hello $this" + +fun String.printHello(): Unit = println("Hello $this") + +fun main() { + val name = "Eko" + + println(name.hello()) + + name.printHello() + + "Kurniawan".printHello() +} \ No newline at end of file diff --git a/src/ForLoop.kt b/src/ForLoop.kt new file mode 100644 index 0000000..b1cb750 --- /dev/null +++ b/src/ForLoop.kt @@ -0,0 +1,17 @@ +fun main() { + + var array = arrayOf("Eko", "Kurniawan", "Khannedy", "Joko", "Budi", "Rudi") + + var total = 0 + for (name in array) { + println(name) + total++ + } + println("Total perulangan = $total") + + val ukuranArray = array.size - 1 + for (i in 0..ukuranArray) { + println("Index $i = ${array.get(i)}") + } + +} \ No newline at end of file diff --git a/src/Function.kt b/src/Function.kt new file mode 100644 index 0000000..459f959 --- /dev/null +++ b/src/Function.kt @@ -0,0 +1,11 @@ + +fun helloWorld(){ + println("Hello Dunia") + println("Selamat Belajar Kotlin") +} + +fun main() { + helloWorld() + helloWorld() + helloWorld() +} \ No newline at end of file diff --git a/src/FunctionDefaultParameter.kt b/src/FunctionDefaultParameter.kt new file mode 100644 index 0000000..6200cd2 --- /dev/null +++ b/src/FunctionDefaultParameter.kt @@ -0,0 +1,14 @@ +fun hello(firstName: String, lastName: String? = null) { + if (lastName == null) { + println("Hello $firstName") + } else { + println("Hello $firstName $lastName") + } +} + +fun main() { + hello("Eko") + hello("Eko", "Kurniawan") + hello("Budi") + hello("Joko", "Nugroho") +} \ No newline at end of file diff --git a/src/FunctionInfixNotation.kt b/src/FunctionInfixNotation.kt new file mode 100644 index 0000000..ccb79a9 --- /dev/null +++ b/src/FunctionInfixNotation.kt @@ -0,0 +1,12 @@ +infix fun String.to(type: String): String { + if (type == "UP") { + return this.toUpperCase() + } else { + return this.toLowerCase() + } +} + +fun main() { + val result = "Eko Kurniawan" to "LO" + println(result) +} \ No newline at end of file diff --git a/src/FunctionNamedArgument.kt b/src/FunctionNamedArgument.kt new file mode 100644 index 0000000..cb802c1 --- /dev/null +++ b/src/FunctionNamedArgument.kt @@ -0,0 +1,16 @@ +fun fullName( + firstName: String, + middleName: String, + lastName: String +) { + println("Hello $firstName $middleName $lastName") +} + +fun main() { + fullName("Eko", "Kurniawan", "Khannedy") + fullName( + lastName = "Khannedy", + firstName = "Eko", + middleName = "Kurniawan" + ) +} \ No newline at end of file diff --git a/src/FunctionParameter.kt b/src/FunctionParameter.kt new file mode 100644 index 0000000..a6e45bd --- /dev/null +++ b/src/FunctionParameter.kt @@ -0,0 +1,13 @@ +fun sayHello(firstName: String, lastName: String?) { + if (lastName == null) { + println("Hello $firstName") + } else { + println("Hello $firstName $lastName") + } +} + +fun main() { + sayHello("Eko", "Kurniawan") + sayHello("Budi", null) + sayHello("Joko", "Nugraha") +} \ No newline at end of file diff --git a/src/FunctionReturnType.kt b/src/FunctionReturnType.kt new file mode 100644 index 0000000..734d378 --- /dev/null +++ b/src/FunctionReturnType.kt @@ -0,0 +1,27 @@ +fun jumlahkan(a: Int, b: Int): Int { + val total = a + b + return total +} + +fun bagi(a: Int, b: Int): Int { + if (b == 0) { + return 0 + } else { + val result = a / b + return result + } +} + +fun main() { + println(jumlahkan(10, 10)) + println(jumlahkan(100, 100)) + + val result = jumlahkan(200, 200) + println(result) + + val result2 = jumlahkan(5, 5) + println(result2) + + println(bagi(100, 10)) + println(bagi(100, 0)) +} \ No newline at end of file diff --git a/src/FunctionScope.kt b/src/FunctionScope.kt new file mode 100644 index 0000000..7c95a0b --- /dev/null +++ b/src/FunctionScope.kt @@ -0,0 +1,19 @@ + + +fun satu(){ + // error + // contohHelloWorld() +} + +fun dua(){ + // error + // contohHelloWorld() +} + +fun main() { + fun contohHelloWorld(){ + println("Hello World") + } + + contohHelloWorld() +} \ No newline at end of file diff --git a/src/FunctionVarargsParameter.kt b/src/FunctionVarargsParameter.kt new file mode 100644 index 0000000..f64f64e --- /dev/null +++ b/src/FunctionVarargsParameter.kt @@ -0,0 +1,16 @@ +fun hitungTotal(name: String, vararg values: Int): Int { + var total = 0 + + for (value in values) { + total += value + } + + return total; +} + +fun main() { + // val values = arrayOf(10,10,10,10) + val result = hitungTotal("Eko", 10, 10, 10) + + println(result) +} \ No newline at end of file diff --git a/src/HelloWorld.kt b/src/HelloWorld.kt new file mode 100644 index 0000000..8826f29 --- /dev/null +++ b/src/HelloWorld.kt @@ -0,0 +1,8 @@ +fun main() { + println("Hello World") + println("Hello World") + println("Hello World") + + print("Hello ") + print("Eko") +} \ No newline at end of file diff --git a/src/HigherOrderFunction.kt b/src/HigherOrderFunction.kt new file mode 100644 index 0000000..da27220 --- /dev/null +++ b/src/HigherOrderFunction.kt @@ -0,0 +1,17 @@ +fun main() { + + fun hello(name: String, transformer: (String) -> String): String { + val nameTransform = transformer(name) + return "Hello $nameTransform" + } + + val lambdaUpper = { value: String -> value.toUpperCase() } + println(hello("Eko", lambdaUpper)) + + println(hello("Eko", { value: String -> value.toLowerCase() })) + + val result1 = hello("Eko", { value: String -> value.toLowerCase() }) + val result2 = hello("Eko") { value: String -> + value.toLowerCase() + } +} \ No newline at end of file diff --git a/src/IfExpression.kt b/src/IfExpression.kt new file mode 100644 index 0000000..b4d0e8f --- /dev/null +++ b/src/IfExpression.kt @@ -0,0 +1,15 @@ +fun main() { + + val nilai = 55 + + if (nilai > 90) { + println("Cool") + } else if (nilai > 75) { + println("Good Job") + } else if (nilai > 60) { + println("Not Bad") + } else { + println("Silahkan coba lagi tahun depan") + } + +} \ No newline at end of file diff --git a/src/InlineFunction.kt b/src/InlineFunction.kt new file mode 100644 index 0000000..8a622e9 --- /dev/null +++ b/src/InlineFunction.kt @@ -0,0 +1,12 @@ +inline fun hello( + firstName: () -> String, + noinline lastName: () -> String +): String { + return "Hello ${firstName()} ${lastName()}" +} + +fun main() { + for (i in 0..100) { + println(hello({ "Eko" }, { "Kurniawan" })) + } +} \ No newline at end of file diff --git a/src/Komentar.kt b/src/Komentar.kt new file mode 100644 index 0000000..05ac724 --- /dev/null +++ b/src/Komentar.kt @@ -0,0 +1,20 @@ +/** + * Function untuk contoh + */ +fun contohFunction(){ + +} + +/** + * Contoh function dengan parameter + * @param name nama parameter + */ +fun contohFunctionParameter(name: String){ + +} + +fun main() { + // memanggil contoh function + contohFunction() + contohFunctionParameter("Eko") // memanggil contoh function parameter +} \ No newline at end of file diff --git a/src/Label.kt b/src/Label.kt new file mode 100644 index 0000000..a01ac65 --- /dev/null +++ b/src/Label.kt @@ -0,0 +1,35 @@ +fun labelBreak() { + loopI@ for (i in 1..10) { + loopJ@ for (j in 1..10) { + if (i > 5) { + break@loopI + } + println("$i * $j = ${i * j}") + } + } +} + +fun labelContinue() { + loopI@ for (i in 1..10) { + loopJ@ for (j in 1..10) { + if (j == 5) { + continue@loopI + } + println("$i * $j = ${i * j}") + } + } +} + +fun main() { + + fun test(name: String, operation: (String) -> Unit): Unit = operation(name) + + test("") test@{ + if (it == "") { + return@test + } else { + println("Eko") + } + } + +} \ No newline at end of file diff --git a/src/LambdaExpression.kt b/src/LambdaExpression.kt new file mode 100644 index 0000000..5b114f3 --- /dev/null +++ b/src/LambdaExpression.kt @@ -0,0 +1,21 @@ +fun toUpper(value: String): String = value.toUpperCase() + +fun main() { + + val contohLambda: (String, String) -> String = { firstName: String, lastName: String -> + val result = "$firstName $lastName" + result + } + + val result = contohLambda("Eko", "Kurniawan") + println(result) + + val sayHello: (String) -> String = { + "Hello $it" + } + + println(sayHello("Eko")) + + val toUpperCase : (String) -> String = ::toUpper + println(toUpperCase("Eko Kurniawan")) +} \ No newline at end of file diff --git a/src/MainParameter.kt b/src/MainParameter.kt new file mode 100644 index 0000000..4bd63cb --- /dev/null +++ b/src/MainParameter.kt @@ -0,0 +1,5 @@ +fun main(args: Array) { + for (value in args) { + println(value) + } +} \ No newline at end of file diff --git a/src/Number.kt b/src/Number.kt new file mode 100644 index 0000000..03815cf --- /dev/null +++ b/src/Number.kt @@ -0,0 +1,20 @@ +fun main() { + var age: Int = 200 + + var sample: Float = 10.11F + + println(age) + println(sample) + + var binary: Int = 0b010101010 + println(binary) + + var price: Long = 9_000_000_000L; + println(price) + + var priceInt: Int = price.toInt() + println(priceInt) + + var doubleBinary: Double = binary.toDouble() + println(doubleBinary) +} \ No newline at end of file diff --git a/src/OperasiBoolean.kt b/src/OperasiBoolean.kt new file mode 100644 index 0000000..a3efbca --- /dev/null +++ b/src/OperasiBoolean.kt @@ -0,0 +1,16 @@ +fun main() { + + val nilaiUjian = 80 + val nilaiAbsen = 80 + val nilaiEkstra = 70 + + val apakahLulusUjian = nilaiUjian > 75 + val apakahLulusAbsen = nilaiAbsen > 75 + val apakahLulusExtra = nilaiEkstra > 75 + + val apakahLulus = apakahLulusUjian && apakahLulusAbsen && apakahLulusExtra + // val apakahLulus = (apakahLulusUjian && apakahLulusAbsen) && apakahLulusExtra + // val apakahLulus = ((apakahLulusUjian && apakahLulusAbsen) && apakahLulusExtra) + println(apakahLulus) + +} \ No newline at end of file diff --git a/src/OperasiPerbandingan.kt b/src/OperasiPerbandingan.kt new file mode 100644 index 0000000..bdf593e --- /dev/null +++ b/src/OperasiPerbandingan.kt @@ -0,0 +1,16 @@ +fun main() { + val a = 100 + val b = 200 + + val result: Boolean = a < b + println(result) + + println(100 >= 100) + println(100 <= 100) + + println("a" < "b") + + println("a" == "a") + + println("a" != "b") +} \ No newline at end of file diff --git a/src/OperatorMatematika.kt b/src/OperatorMatematika.kt new file mode 100644 index 0000000..18672b7 --- /dev/null +++ b/src/OperatorMatematika.kt @@ -0,0 +1,33 @@ +fun main() { + + val result = 10.0 / 3.0 + println(result) + + val result2 = 10 + 10 / 2 + println(result2) + + var total: Int = 0 + + val barang1 = 100 + total += barang1 + + val barang2 = 200 + total += barang2 + + val barang3 = 300 + total += barang3 + + println(total) + + total++ // total = total + 1 + total++ // total = total + 1 + total++ // total = total + 1 + println(total) + + val suhu = -5 + println(suhu) + + val sehat = true + println(!sehat) + +} \ No newline at end of file diff --git a/src/Package.kt b/src/Package.kt new file mode 100644 index 0000000..88131d9 --- /dev/null +++ b/src/Package.kt @@ -0,0 +1,8 @@ +import com.company.belajar.util.* + +fun main() { + sayHello("Eko") + sayGoodBye("Eko") + + // com.company.belajar.util.sayGoodBye("Eko") +} \ No newline at end of file diff --git a/src/Range.kt b/src/Range.kt new file mode 100644 index 0000000..5f5f0b3 --- /dev/null +++ b/src/Range.kt @@ -0,0 +1,12 @@ +fun main() { + + // val range = 1..100 + val range = 100 downTo 1 step 2 + + println(range.count()) + println(range.contains(50)) + println(range.contains(200)) + println(range.first) + println(range.last) + println(range.step) +} \ No newline at end of file diff --git a/src/RecursiveFunction.kt b/src/RecursiveFunction.kt new file mode 100644 index 0000000..64457a6 --- /dev/null +++ b/src/RecursiveFunction.kt @@ -0,0 +1,19 @@ +fun main() { + + fun factorialLoop(value: Int): Int { + var result = 1 + for (i in value downTo 1) { + result *= i + } + return result + } + println(factorialLoop(10)) + + fun factorialRecursive(value: Int): Int { + return when (value) { + 1 -> 1 + else -> value * factorialRecursive(value - 1) + } + } + println(factorialRecursive(10)) +} \ No newline at end of file diff --git a/src/ReturnIfWhen.kt b/src/ReturnIfWhen.kt new file mode 100644 index 0000000..367eaa7 --- /dev/null +++ b/src/ReturnIfWhen.kt @@ -0,0 +1,19 @@ +fun main() { + + fun sayHello(name: String = ""): String { + return when(name){ + "" -> "Hello Bro!" + else -> "Hello $name" + } +// return if (name == "") { +// "Hello Bro!" +// } else { +// "Hello $name" +// } + } + + + println(sayHello()) + println(sayHello("Eko")) + +} \ No newline at end of file diff --git a/src/SingleExpressionFunction.kt b/src/SingleExpressionFunction.kt new file mode 100644 index 0000000..fb489ed --- /dev/null +++ b/src/SingleExpressionFunction.kt @@ -0,0 +1,8 @@ +fun kaliDua(a: Int): Int = a * 2 + +fun sapa(name: String): Unit = println("Hello $name") + +fun main() { + println(kaliDua(10)) + sapa("Eko") +} \ No newline at end of file diff --git a/src/String.kt b/src/String.kt new file mode 100644 index 0000000..56d3b1b --- /dev/null +++ b/src/String.kt @@ -0,0 +1,22 @@ +fun main() { + + var firstName: String = "Eko" + var lastName: String = "Khannedy" + + var address: String = """ + >Jalan belum jadi, RT 01 RW 01, + >Kabupaten SUbang, + >Jawa barat, + >Indonesia + """.trimMargin(">") + + println(firstName) + println(lastName) + println(address) + + var fullName: String = "$firstName $lastName" + println(fullName) + + var desc: String = "$fullName length = ${fullName.length}" + println(desc) +} \ No newline at end of file diff --git a/src/TailRecursiveFunction.kt b/src/TailRecursiveFunction.kt new file mode 100644 index 0000000..72db527 --- /dev/null +++ b/src/TailRecursiveFunction.kt @@ -0,0 +1,19 @@ +fun main() { + + tailrec fun display(value: Int) { + println("Recursive $value") + if (value > 0) { + display(value - 1) + } + } + + tailrec fun factorialRecursive(value: Int, total: Int = 1): Int { + return when (value) { + 1 -> total + else -> factorialRecursive(value - 1, total * value) + } + } + + println(factorialRecursive(10)) + +} \ No newline at end of file diff --git a/src/UnitReturningFunction.kt b/src/UnitReturningFunction.kt new file mode 100644 index 0000000..96a2364 --- /dev/null +++ b/src/UnitReturningFunction.kt @@ -0,0 +1,7 @@ +fun printHello(): Unit { + println("Hello") +} + +fun main() { + printHello() +} \ No newline at end of file diff --git a/src/Variable.kt b/src/Variable.kt new file mode 100644 index 0000000..c536267 --- /dev/null +++ b/src/Variable.kt @@ -0,0 +1,18 @@ +const val APPLICATION = "Belajar Kotlin" +const val VERSION = "0.0.1" + +fun main() { + + val firstName:String = "Eko" + val age = 30 + + println(firstName) + println(age) + + var name: String? = null + name = "Eko" + println(name) + println(name?.length) + + println("$APPLICATION : $VERSION") +} \ No newline at end of file diff --git a/src/WhenExpression.kt b/src/WhenExpression.kt new file mode 100644 index 0000000..8a52498 --- /dev/null +++ b/src/WhenExpression.kt @@ -0,0 +1,53 @@ +fun main() { + + var nilai = "B" + + when (nilai) { + "A" -> { + println("Amazing") + } + "B" -> { + println("Good") + } + "C" -> { + println("Not Bad") + } + "D" -> { + println("Bad") + } + else -> { + println("Try Again Next Year") + } + } + + when (nilai) { + // if(nilai == "A" || nilai == "B" || nilai == "C") + "A", "B", "C" -> { + println("Selamat Anda Lulus") + } + else -> { + println("Maaf, Anda Tidak Lulus") + } + } + + nilai = "E" + val nilaiLulus: Array = arrayOf("A", "B", "C") + when (nilai) { + in nilaiLulus -> println("SELAMAT ANDA LULUS") + !in nilaiLulus -> println("MAAF, TIDAK LULUS") + } + + val name = "Eko" + when (name) { + is String -> println("Name is String") + !is String -> println("Name is not String") + } + + val nilaiUjian = 91 + when { + nilaiUjian > 90 -> println("Amazing") + nilaiUjian > 80 -> println("Good Job") + nilaiUjian > 60 -> println("Not Bad") + else -> println("Ngulang tahun depan") + } +} \ No newline at end of file diff --git a/src/WhileLoop.kt b/src/WhileLoop.kt new file mode 100644 index 0000000..175e837 --- /dev/null +++ b/src/WhileLoop.kt @@ -0,0 +1,12 @@ +fun main() { + + var i = 0 + + while (i < 10){ + println("Perulangan $i") + i++ + } + + println("Selesai perulangan") + +} \ No newline at end of file diff --git a/src/com/company/belajar/util/StringUtil.kt b/src/com/company/belajar/util/StringUtil.kt new file mode 100644 index 0000000..71fe97a --- /dev/null +++ b/src/com/company/belajar/util/StringUtil.kt @@ -0,0 +1,9 @@ +package com.company.belajar.util + +fun sayHello(name: String) { + println("Hello $name") +} + +fun sayGoodBye(name: String) { + println("Goodbye $name") +} \ No newline at end of file