Created
August 8, 2018 17:48
-
-
Save labohkip81/a4de5669b717f011b45bc97536b729c0 to your computer and use it in GitHub Desktop.
Kotlin practice files.Codes are well commented thus can be used for revision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Kotlin2JvmCompilerArguments"> | |
<option name="jvmTarget" value="1.8" /> | |
</component> | |
<component name="KotlinCommonCompilerArguments"> | |
<option name="apiVersion" value="1.2" /> | |
<option name="languageVersion" value="1.2" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="libraryTable"> | |
<library name="KotlinJavaRuntime"> | |
<CLASSES> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8.jar!/" /> | |
</CLASSES> | |
<JAVADOC /> | |
<SOURCES> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-sources.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-reflect-sources.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-test-sources.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk7-sources.jar!/" /> | |
<root url="jar://$KOTLIN_BUNDLED$/lib/kotlin-stdlib-jdk8-sources.jar!/" /> | |
</SOURCES> | |
</library> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/Packt book projects.iml" filepath="$PROJECT_DIR$/Packt book projects.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
<orderEntry type="library" name="KotlinJavaRuntime" level="project" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this creates a specified number of nulls | |
fun main(args:Array<String>){ | |
var laban = arrayOfNulls<Int>(3) | |
println( | |
//this will print out null values | |
laban | |
) | |
//perfomong direct calculations | |
var Arraytest = Array(5){it*3} | |
println(Arraytest.toIntArray()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program sill try to show how arrays are represented | |
//arrays are treated as any other data types in kotlin | |
//they start indexing from zero | |
//thats why on referencing index number zero the value 1 is returned | |
fun main(args:Array<String>){ | |
val laban = longArrayOf(1,2,3,4,5,6,7,8,5) | |
println(laban[0]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program performs basic conversion like below it performs conversion of Int to string | |
fun main(args:Array<String>){ | |
var laban = 50 | |
println(laban.toString()+5) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program shows the basic functionality of the for loop | |
fun main(args:Array<String>){ | |
val Array1 = arrayOf(1,2,3,4,5) | |
for( item in Array1) | |
println(item) | |
for(i in Array1.indices) | |
println(Array1[2]) | |
//this section show the while loop functionality | |
var numbers = 1..5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program calculates addition of two numbers by accessing the formula from another function | |
//one thing about functions in java is that they have to be declared outside the main function | |
fun main(args:Array<String>){ | |
//calling the function and passing arguments to the parameters defined. | |
doSum( | |
1, | |
2 | |
) | |
doSum( | |
24, | |
47 | |
) | |
} | |
//declaring the second function | |
fun doSum(num1:Int,num2:Int){ | |
var sum = num1+num2 | |
println(sum) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program shows the usage of the if statement in kotlin | |
fun main(args:Array<String>){ | |
val x=90 | |
println(if(x>100) "greater" else "smaller")//this shows when if is used as an expression | |
if (x>100){ //this shows when if has been used as a statement | |
println("greater") | |
} | |
else { | |
println("Smaller") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program shows he functionality of the ++ and -- operators and they come in two forms | |
//pre and post those defined before and after the expressions | |
fun main(args:Array<String>){ | |
var num1 =1 | |
println(num1++)/* prints 1 */ | |
println(num1)//prints2 | |
println(num1++)//prints 2 | |
println(num1)//this prints 3 | |
//the above adds the value of num1 to itself thus showing the functionality of the increment operator | |
var num2 = 2 | |
println(++num2) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args:Array<String>){ | |
} | |
fun printSum(a: Int, b: Int) { | |
if(a < 0 || b < 0) { | |
return | |
} | |
val sum = a + b | |
print(sum) | |
// 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args:Array<String>){ | |
var a: Int = 1 // 1 | |
//the first line is stored as a primitive data type and thus the JVM takes it for a normal java types | |
var b: Int? = null // 2 | |
//the second line can store a null variable and thus it is stored as a boxed representation | |
b = 12 // 3 | |
//b is still stored as a boxed since as per the first declaration b could store a null value | |
var c = 0b00010 | |
//an extra point to note the 0b literal represents binary digits | |
var d = 0xF213A | |
//the literal 0x represents hexadecimal values | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
val num1: Int = 100 | |
val num2 = 1..1000 | |
println("${num1 in num2} range") | |
//ranges are closed end meaning that the end value is also inclusive | |
val num3 = 1..1 | |
for (i in num3) { | |
println(i) | |
//for showing a decrementing range we use the down to function | |
val num4 = 10 downTo 1 | |
for (a in num4) | |
print(a) | |
println() | |
//the code above will print the values of the range 1-10 in a reverse decrementing order | |
//below we try to define a range with skipping of values | |
val num5 = 1..10 | |
for (b in num5 step 2) { | |
println(b) | |
//step with a decrementation | |
val num6 = 9 downTo 1 | |
for(c in num6 step 3 ) { | |
println(c) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.reflect.jvm.internal.impl.serialization.deserialization.FlexibleTypeDeserializer | |
//this program demonstrates the functionality of single expression functions. | |
fun main(args:Array<String>){ | |
val laban = square(5) | |
val num1 =squares(7) | |
println(laban) | |
println("This prints the square result from the single line expression : $num1") | |
/* this prints the values returned by the add function. */ | |
val names= add("Laban","Kiplagat",c = "Kosgei") | |
println("Your camelCase names are $names") | |
} | |
//the function below is a block expression function that perfoms a multiplication of x by x | |
tailrec fun square(x: Int): Int { | |
return x * x | |
} | |
//below is a reduced function to a single line expression | |
fun squares(y:Int):Int=y*y | |
//more practice on the same....using type string | |
fun add(a:String,b:String,c:String):String = a+b+c | |
//function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program tries to explain the idea of strict null safety in kotlin | |
//it also shows how a null can be made. | |
fun main(args:Array<String>){ | |
//declaring a null normally will bring an error, see the commented code below | |
//val laban = null: this during compiling will bring forth an error | |
var laban : String? = null | |
println(laban) | |
//as you see above the ? sign is used to allow a variable store a null value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program is for purposes of revision | |
//this program explains more on the usage of any. | |
//this is where the user can declare a value to a string and later on assign an int to it | |
fun main(args:Array<String>){ | |
//define a type | |
var laban = '1' | |
//to check the assigned value use the buttons ctl + Shift + p | |
var title:Any = "Laban" | |
//above it has been declared to a String | |
//Below its being updated as an int | |
title = 100 | |
println("The value of title is $title") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args:Array<String>){ | |
val (a,b) = Pair("LAban","Kiplagat") | |
//this assigns both on one line | |
println(a) | |
println(b) | |
//this assigns laban both an int and a string | |
val laban = "Kiplagat" to 4400 | |
println(laban) | |
println(laban.second*50) | |
//defining a long data type | |
val longNum = 30L | |
//also can be defined as follows | |
val longNum2 : Long = 35L | |
println(longNum + longNum2) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this program defines the functionality of the vararg parameter where the number of arguments to be passed are not known | |
fun main(args: Array<String>) { | |
//calling the function that will add all the passed arguments | |
laban(1, 2, 2, 5, 6, 7, 8, 9, 1) | |
printAll("A", "B", "C", "D", "E", "F", "Laban") | |
varargAsSubType("1",4,5,"60") | |
} | |
//define a function with no specified number of arguments | |
fun laban(vararg laban: Int) { | |
println() | |
println(message = laban.sum()) | |
} | |
//Defining a function using vararg using strings | |
fun printAll(vararg texts: String) { | |
//Inferred type of texts is Array<String> | |
val allTexts = texts.joinToString(":").toUpperCase() | |
println() | |
println("Texts are $allTexts") | |
} | |
//vararg can be used as a subtype of other data types | |
fun varargAsSubType( vararg laban:Any){ | |
val fedNum = laban. joinToString(";") | |
println() | |
println(fedNum) | |
} | |
//also as a reminder the joinToString function is used to joined a number of specified arguments to one string value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using the when expression | |
//this program tries to show how the when statement is used in place of the normal switch case in java | |
//a when statement to be complete has to have an else statement | |
fun main(args: Array<String>) { | |
val vehicle = "Car" | |
val message = when (vehicle) { | |
"Bike" -> { | |
" has two wheels" | |
} | |
"Car" -> { | |
" has for wheels" | |
} | |
else -> | |
" has unknown number of wheels" | |
} | |
println("The $vehicle has $message") | |
//checking data types\ | |
val person = "o" | |
val name1 = when (person) { | |
is String -> person.toUpperCase() | |
else -> person.toLowerCase() | |
} | |
println(name1) | |
val riskManagement = 40 | |
val riskType1 = when (riskManagement) { | |
in 21..40 -> "Risk is very high" | |
in 33..60 -> "Risk is extremely high" | |
in 1..20 -> "Risk is low and thus negligible" | |
else -> "Risk is not defined" | |
} | |
println(riskType1) | |
//Taking into consideration an else statement has more than one options......the when can still branch | |
val riskManagement2:Any = "none" | |
val riskType2 =when(riskManagement2){ | |
in 21..40 -> "Risk is very high" | |
in 33..60 -> "Risk is extremely high" | |
in 1..20 -> "Risk is low and thus negligible" | |
else -> when(riskManagement2){ | |
"Warn"->"warning!! risk oncoming" | |
"Ignore"->"All the risks have been ignored" | |
else -> "Implementation strategy is not known at all" | |
} | |
} | |
println(riskType2) | |
//using when without a branch | |
val laban :Boolean = true | |
val risk = when(laban){ | |
true -> "You are true" | |
false -> "You are false" | |
} | |
println(risk) | |
} | |
fun laban(name:Any):Int = 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this shows a basic functionality of functions in kotlin | |
//parameters are the data variable to be returned by a function | |
//for example in the function presentGently V is a parameter | |
//while the values passed to the method presentGently eg. "100" is an argument | |
//these terms are always used interchangeably in the programming world which is a bad practice. | |
fun main(args:Array<String>){ | |
presentGently("100") | |
presentGently(null) | |
// Prints: Hello. I would like to present you: null | |
presentGently(1) | |
// Prints: Hello. I would like to present you: 1 | |
presentGently("Str") | |
// Prints: Hello. I would like to present you: Str | |
//calling the second function laban and passing arguments for each of the parameters | |
labanName( | |
"Laban", | |
"Kiplagat" | |
) | |
} | |
fun presentGently(v: Any?) { | |
println("Hello. I would like to present you: $v") | |
} | |
fun labanName(firstName:String,secondName:String){ | |
println("Your full names are $firstName $secondName") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment