├── .gitattributes ├── .gitignore ├── Chapter01 ├── ArrayDeclaration.kts ├── ArrayLength.kts ├── ArrayValueAssignment.kts ├── ArrayValueReferencing.kts ├── BooleanAssignment.kts ├── BreakAndContinue.kts ├── CharAssignment.kts ├── ClassDeclaration.kts ├── CompanionObject1.kt ├── CompanionObject2.kt ├── DoWhileLoop.kts ├── DoubleAssignment.kts ├── FloatAssignment.kts ├── ForLoop.kts ├── FunctionDeclaration.kts ├── FunctionDeclaration2.kt ├── FunctionInvokation.kt ├── FunctionReturn.kt ├── HelloApp.zip ├── IfExpression.kts ├── IfExpression2.kts ├── Import Examples │ ├── Buffalo.kt │ ├── Lion.kt │ └── app.kt ├── NullPointerExample.java ├── NullSafety1.kts ├── NullSafety2.kts ├── NullSafety3.kts ├── NumSum.kts ├── Properties.kts ├── StringAssignment.kts ├── TernaryOperator.kts ├── VariableAssignment.kt ├── VariableScope.kt ├── WhileExpression.kt ├── WhileLoop.kts └── hello.kt ├── Chapter02and03 └── Tetris.zip ├── Chapter04 └── messenger-api.zip ├── Chapter05and06 └── Messenger.zip ├── Chapter07 ├── ContentProviderExample.zip ├── RoomExample.zip └── StorageExamples.zip ├── Chapter09and10 └── Place-Reviewer.zip ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter01/ArrayDeclaration.kts: -------------------------------------------------------------------------------- 1 | val names = arrayOf("Tobi", "Tonia", "Timi") 2 | -------------------------------------------------------------------------------- /Chapter01/ArrayLength.kts: -------------------------------------------------------------------------------- 1 | val numbers = arrayOf(1, 2, 3, 4) 2 | println(numbers.length) // Prints 4 3 | -------------------------------------------------------------------------------- /Chapter01/ArrayValueAssignment.kts: -------------------------------------------------------------------------------- 1 | val numbers = arrayOf(1, 2, 3, 4) 2 | println(numbers[0]) // Prints 1 3 | numbers[0] = 23 4 | println(numbers[0]) // Prints 23 5 | -------------------------------------------------------------------------------- /Chapter01/ArrayValueReferencing.kts: -------------------------------------------------------------------------------- 1 | val numbers = arrayOf(1, 2, 3, 4) 2 | println(numbers[0]) // Prints 1 3 | println(numbers.get(1)) // Prints 2 4 | -------------------------------------------------------------------------------- /Chapter01/BooleanAssignment.kts: -------------------------------------------------------------------------------- 1 | var t: Boolean = true 2 | var f: Boolean = false 3 | -------------------------------------------------------------------------------- /Chapter01/BreakAndContinue.kts: -------------------------------------------------------------------------------- 1 | data class Student(val name: String, val age: Int, val school: String) 2 | 3 | val prospectiveStudents: ArrayList = ArrayList() 4 | val admittedStudents: ArrayList = ArrayList() 5 | 6 | prospectiveStudents.add(Student("Daniel Martinez", 12, "Hogwarts")) 7 | prospectiveStudents.add(Student("Jane Systrom", 22, "Harvard")) 8 | prospectiveStudents.add(Student("Matthew Johnson", 22, "University of Maryland")) 9 | prospectiveStudents.add(Student("Jide Sowade", 18, "University of Ibadan")) 10 | prospectiveStudents.add(Student("Tom Hanks", 25, "Howard University")) 11 | 12 | for (student in prospectiveStudents) { 13 | if (student.age < 16) { 14 | continue 15 | } 16 | admittedStudents.add(student) 17 | 18 | if (admittedStudents.size >= 3) { 19 | break 20 | } 21 | } 22 | 23 | println(admittedStudents) 24 | -------------------------------------------------------------------------------- /Chapter01/CharAssignment.kts: -------------------------------------------------------------------------------- 1 | val c: Char = 'i' // I am a character 2 | -------------------------------------------------------------------------------- /Chapter01/ClassDeclaration.kts: -------------------------------------------------------------------------------- 1 | class HelloPrinter { 2 | fun printHello() { 3 | println("Hello!") 4 | } 5 | } 6 | 7 | val printer = HelloPrinter() 8 | printer.printHello() // Prints hello 9 | -------------------------------------------------------------------------------- /Chapter01/CompanionObject1.kt: -------------------------------------------------------------------------------- 1 | class Printer { 2 | companion object { 3 | fun printDocument() = println("Document printing successful.") 4 | } 5 | } 6 | 7 | fun main(args: Array) { 8 | Printer.printDocument() // printDocument() invoked via companion object 9 | Printer.Companion.printDocument() // also invokes printDocument() via 10 | // a companion object 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/CompanionObject2.kt: -------------------------------------------------------------------------------- 1 | class Printer { 2 | companion object DocumentPrinter { // Companion object identified by DocumentPrinter 3 | fun printDocument() = println("Document printing successful.") 4 | } 5 | } 6 | 7 | fun main(args: Array) { 8 | Printer.DocumentPrinter.printDocument() // printDocument() invoked via 9 | // a named companion object 10 | } 11 | -------------------------------------------------------------------------------- /Chapter01/DoWhileLoop.kts: -------------------------------------------------------------------------------- 1 | var i = 0 2 | 3 | do { 4 | println("I'm in here!") 5 | i++ 6 | } while (i < 10) 7 | 8 | println("I'm out here!") 9 | -------------------------------------------------------------------------------- /Chapter01/DoubleAssignment.kts: -------------------------------------------------------------------------------- 1 | var d: Double = 3.142 2 | -------------------------------------------------------------------------------- /Chapter01/FloatAssignment.kts: -------------------------------------------------------------------------------- 1 | var pi: Float = 3.142 2 | -------------------------------------------------------------------------------- /Chapter01/ForLoop.kts: -------------------------------------------------------------------------------- 1 | val numSet = arrayOf(1, 563, 23) 2 | 3 | for (number in numSet) { 4 | println(number) 5 | } 6 | -------------------------------------------------------------------------------- /Chapter01/FunctionDeclaration.kts: -------------------------------------------------------------------------------- 1 | fun printSum(a: Int, b: Int) { 2 | print(a + b) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/FunctionDeclaration2.kt: -------------------------------------------------------------------------------- 1 | fun printSum(a: Int, b: Int): Unit { 2 | print(a + b) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/FunctionInvokation.kt: -------------------------------------------------------------------------------- 1 | fun repeat(word: String, times: Int) { 2 | var i = 0 3 | 4 | while (i < times) { 5 | println(word) 6 | i++ 7 | } 8 | } 9 | 10 | fun main(args: Array) { 11 | repeat("Hello!", 5) 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/FunctionReturn.kt: -------------------------------------------------------------------------------- 1 | fun returnFullName(firstName: String, surname: String): String { 2 | return "${firstName} ${surname}" 3 | } 4 | 5 | fun main(args: Array) { 6 | val fullName: String = returnFullName("James", "Cameron") 7 | println(fullName) // prints: James Cameron 8 | } 9 | -------------------------------------------------------------------------------- /Chapter01/HelloApp.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter01/HelloApp.zip -------------------------------------------------------------------------------- /Chapter01/IfExpression.kts: -------------------------------------------------------------------------------- 1 | val a = 1 2 | 3 | if (a == 1) { 4 | print("a is one") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter01/IfExpression2.kts: -------------------------------------------------------------------------------- 1 | val a = 4 2 | 3 | if (a == 1) { 4 | print("a is equal to one.") 5 | } else if (a == 2) { 6 | print("a is equal to two.") 7 | } else { 8 | print("a is neither one nor two.") 9 | } 10 | -------------------------------------------------------------------------------- /Chapter01/Import Examples/Buffalo.kt: -------------------------------------------------------------------------------- 1 | package animals 2 | data class Buffalo(val mass: Int, val maxSpeed: Int, var isDead: Boolean = false) 3 | -------------------------------------------------------------------------------- /Chapter01/Import Examples/Lion.kt: -------------------------------------------------------------------------------- 1 | package animals 2 | 3 | class Lion(val mass: Int, val maxSpeed: Int) { 4 | 5 | fun kill(animal: Buffalo) { // Buffalo type used with our import 6 | if (!animal.isDead) { 7 | println("Lion attacking animal.") 8 | animal.isDead = true 9 | println("Lion kill successful.") 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/Import Examples/app.kt: -------------------------------------------------------------------------------- 1 | import animals.Buffalo 2 | import animals.Lion 3 | 4 | fun main(args: Array) { 5 | val lion = Lion(190, 80) 6 | val buffalo = Buffalo(620, 60) 7 | println("Buffalo is dead: ${buffalo.isDead}") 8 | lion.kill(buffalo) 9 | println("Buffalo is dead: ${buffalo.isDead}") 10 | } 11 | -------------------------------------------------------------------------------- /Chapter01/NullPointerExample.java: -------------------------------------------------------------------------------- 1 | class NullPointerExample { 2 | 3 | public static void main(String[] args) { 4 | String name = “James Gates”; 5 | System.out.println(name.length()); // Prints 11 6 | 7 | name = null; // assigning a value of null to name 8 | System.out.println(name.length()); // throws NullPointerException 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Chapter01/NullSafety1.kts: -------------------------------------------------------------------------------- 1 | var name: String = "James Gates" 2 | println(name.length) 3 | 4 | name = null // null value assignment not permitted 5 | println(name.length) 6 | -------------------------------------------------------------------------------- /Chapter01/NullSafety2.kts: -------------------------------------------------------------------------------- 1 | var name: String? = "James" 2 | println(name.length) 3 | 4 | name = null // null value assignment permitted 5 | println(name.length) 6 | -------------------------------------------------------------------------------- /Chapter01/NullSafety3.kts: -------------------------------------------------------------------------------- 1 | var name: String? = "James" 2 | println(name?.length) 3 | 4 | name = null // null value assignment permitted 5 | println(name?.length) 6 | -------------------------------------------------------------------------------- /Chapter01/NumSum.kts: -------------------------------------------------------------------------------- 1 | val x: Int = 1 2 | val y: Int = 2 3 | val z: Int = x + y 4 | println(z) 5 | -------------------------------------------------------------------------------- /Chapter01/Properties.kts: -------------------------------------------------------------------------------- 1 | class Person { 2 | var age = 0 3 | var firstName = "" 4 | var surname = "" 5 | } 6 | 7 | val person = Person() 8 | person.firstName = "Raven" 9 | person.surname = "Spacey" 10 | person.age = 35 11 | -------------------------------------------------------------------------------- /Chapter01/StringAssignment.kts: -------------------------------------------------------------------------------- 1 | val martinLutherQuote: String = "Free at last, Free at last, Thank God almighty we are free at last." 2 | -------------------------------------------------------------------------------- /Chapter01/TernaryOperator.kts: -------------------------------------------------------------------------------- 1 | val firstName = "Jon" 2 | val surname = firstName.equals("Jon") ? "Snow" : "Doe" 3 | -------------------------------------------------------------------------------- /Chapter01/VariableAssignment.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | var x: Int = 1 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/VariableScope.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | // block A begins 3 | var a = 10 4 | var i = 1 5 | 6 | while (i < 10) { 7 | // block B begins 8 | val b = a / i 9 | print(b) 10 | i++ 11 | } 12 | print(b) // Error occurs: variable b is out of scope 13 | } 14 | -------------------------------------------------------------------------------- /Chapter01/WhileExpression.kt: -------------------------------------------------------------------------------- 1 | fun printEvenNumbers(numbers: Array) { 2 | numbers.forEach { 3 | when (it % 2) { 4 | 0 -> println(it) 5 | } 6 | } 7 | } 8 | 9 | fun main (args: Array) { 10 | val numberList: Array = arrayOf(1, 2, 3, 4, 5, 6) 11 | printEvenNumbers(numberList) 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/WhileLoop.kts: -------------------------------------------------------------------------------- 1 | val names = arrayOf("Jeffrey", "William", "Golding", "Segun", "Bob") 2 | var i = 0 3 | 4 | while (!names[i].equals("Segun")) { 5 | println("I am not Segun.") 6 | i++ 7 | } 8 | -------------------------------------------------------------------------------- /Chapter01/hello.kt: -------------------------------------------------------------------------------- 1 | fun main (args: Array) { 2 | println(“Hello world!”) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter02and03/Tetris.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter02and03/Tetris.zip -------------------------------------------------------------------------------- /Chapter04/messenger-api.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter04/messenger-api.zip -------------------------------------------------------------------------------- /Chapter05and06/Messenger.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter05and06/Messenger.zip -------------------------------------------------------------------------------- /Chapter07/ContentProviderExample.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter07/ContentProviderExample.zip -------------------------------------------------------------------------------- /Chapter07/RoomExample.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter07/RoomExample.zip -------------------------------------------------------------------------------- /Chapter07/StorageExamples.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter07/StorageExamples.zip -------------------------------------------------------------------------------- /Chapter09and10/Place-Reviewer.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-Programming-By-Example/794c281864f49160c4bdacc5e4d6aa159aa1b53a/Chapter09and10/Place-Reviewer.zip -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Kotlin Programming By Example 5 | This is the code repository for [Kotlin Programming By Example](https://www.packtpub.com/application-development/kotlin-programming-example?utm_source=github&utm_medium=repository&utm_campaign=9781788474542), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 6 | ## About the Book 7 | Kotlin greatly reduces source code verbosity; with the recently announced first class support from the Android team, a great opportunity has been created. The book will help you learn how to create apps with Kotlin from scratch and get them up-and-running. 8 | 9 | Starting with an introduction to Kotlin, this book will take you through building blocks such as functions, classes, and more. Various features of Kotlin will be explored by building three application of varying complexity. For a quick start to Android development we look at building a classic game Tetris and elaborate on Object Oriented Programming in Kotlin. Our next application is a messenger app, a level up in complexity; we will design and implement the backend as well. Before we move to the third app, we take a look at methods of data persistence, helping us learn storage and retrieval of useful application. Our final app is a Place reviewer; a web application will be making use of the Google Maps API and Placepicker. 10 | 11 | By end of the book you will have gained experience of how to create and deploy Android applications using Kotlin. 12 | ## Instructions and Navigation 13 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 14 | 15 | Chapter 8 doesnot have code files. Chapter 2 and 3, Chapter 5 and 6, Chapter 9 and 10 codes are combined in a single folder. 16 | 17 | The code will look like the following: 18 | ``` 19 | release { 20 | storeFile file("../my-release-key.jks") 21 | storePassword "password" 22 | keyAlias "my-alias" 23 | keyPassword "password" 24 | } 25 | ``` 26 | 27 | If you are a beginner, begin this book with an open mind and a drive to learn. You may find the task of learning a new language daunting at first, but with perseverance, you will be a professional in no time. Read through each chapter in the order they are presented in. This will ensure you grasp all of the content within the book. Take special care to read through each code snippet so as to fully understand what is being done. Implement and run each program in this book yourself. 28 | 29 | ## Related Products 30 | * [Kotlin Blueprints](https://www.packtpub.com/application-development/kotlin-blueprints?utm_source=github&utm_medium=repository&utm_campaign=9781788390804) 31 | 32 | * [Hands-On Android UI Development](https://www.packtpub.com/application-development/hands-android-ui-development?utm_source=github&utm_medium=repository&utm_campaign=9781788475051) 33 | 34 | * [Complete Bootstrap: Responsive Web Development with Bootstrap 4](https://www.packtpub.com/web-development/complete-bootstrap-responsive-web-development-bootstrap-4?utm_source=github&utm_medium=repository&utm_campaign=9781788833400) 35 | 36 | ### Download a free PDF 37 | 38 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
39 |

https://packt.link/free-ebook/9781788474542

--------------------------------------------------------------------------------