Lesson 49: – Package and Import – kotlin Programming language

Lesson 49: – Package and Import – kotlin Programming language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

The packages allow us to group classes, functions, objects, constants, etc. In a namespace for ease of use and maintenance.

The packages group functionalities on a specific topic, for example, functionalities for the access to database, visual interfaces, data encryption, access to files, communications on the web, etc.

Packages are a very effective way of organizing our code to be used in our project or reused by others.

The standard Kotlin Programming language bookstore is organized in packages, the main ones are:

Kotlin
Kotlin.annotation
Kotlin.collections
Kotlin.comparisons
Kotlin.io
Kotlin.ranges
Kotlin.sequences
Kotlin.text

All these packages are imported automatically when we compile our application.

For example, the package kotlin.collections has declarations of classes and interfaces: List, MutableList etc.

We will now see how to create our own package and then consume its contents.

Problem 1

Create a package called math and define two functions inside that allow adding and subtracting two values of type Int.

Then import the package from our main program and call those functions.

The first step after creating Project190 we placed in the src folder, we press the right mouse button and select New -> Package:

Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language -

In the dialog that appears we enter the name of the package to create “math”:

Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language -

Now inside this folder we proceed to create a file called “functions” by right-clicking on the “math” folder we just created:

Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language -

The file that we will create in this package will be called “functions” (a package can have many files):

Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language -

Now we have the file where we will encode the add and subtract functions.

We proceed:

Project190 – funcions.kt

package Mathematics

/**
 * Created by Robort on 8/4/2017.
 */

fun sum (value1: Int, value2: Int) = value1 + value2

fun  subtract (value1: Int, value2: Int) = value1 - value2

The package name goes in the first line of the source code after the keyword package.

Now we will encode our main program where we will consume the functionality of this package. We will create the Main.tk file in the src folder as usual:

Project190 – Main.kt

package Mathematics

/**
 * Created by Robort on 8/4/2017.
 */

fun main (args: Array <String>) {
    val summ = sum (5, 7)
    println ("The sum is $summ")
    val sub = subtract (10, 3)
    println ("Subtraction is $sub")
}

To import all functions defined in the math package we use the keyword import followed by the name of the package and then a dot and the asterisk:

Import math. *

Once the complete package has been imported, we can call its functions:

    Val su = sum (5, 7)
    ...
    Val re = subtract (10, 3)

If we do not do the import a syntactic error is presented when we want to compile the program:

Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language - Lesson 49: - Package and Import - kotlin Programming language -

There are several variants when we use import. The first is that we can import for example only the subtraction function of the mathematical package:

Import matematica.restar

Fun main (args: Array <String>) {
    Val re = subtract (10, 3)
    Println ("Subtraction is $ re")
}

As we see then indicate the name of the package the function to import. We can only call the subtract function in our algorithm.

When we import a functionality we can create an alias, in the following example we import the subtract function and rename it with the following subquery:

Import mathematics.

Fun main (args: Array <String>) {
    Val re = subtract (10, 3)
    Println ("Subtraction is $ re")
}

This is useful when we import, for example, two-packet functions that have the same name.

If we do not have the import the other possibility is to indicate the package before the name of the function (this is very cumbersome if we have to access many functionalities of a package):

Fun main (args: Array <String>) {
    Val summ = mathematics.sum (5, 7)
    Println ("The sum is $summ")
    Val sub= mathematics.subtract(10, 3)
    Println ("Subtraction is $sub")
}

Proposed problem

  • Create a package called inboundcollate. Within the package create a file called input.kt and define the following functions:
    Package inbox
    fun returnInt (message: String): Int {
        print (message)
        return readLine () !!. toInt ()
    }
    
    fun returnDouble (message: String): Double {
        print (message)
        return readLine () !!. toDouble ()
    }
    
    fun returnFloat (message: String): Float {
        print (message)
        return readLine () !!. toFloat ()
    }

    In the main program (Main.tk) import the package inbound call and call several of its functions.

Project191

inbox.kt

Package inbox
fun returnInt (message: String): int {
    print (message)
    return readLine () !!. toInt ()
}

fun returnDouble (message: String): Double {
    print (message)
    return readLine () !!. toDouble ()
}

fun returnFloat (message: String): Float {
    print (message)
    return readLine () !!. toFloat ()
}
Home.kt

Import inbound. *
fun main (args: Array <String>) {
    val number1 = returnInt ("Enter first value:")
    val number2 = returnInt ("Enter second value")
    println ("The sum is ${number1 + number2}")
}

About The Author

Related posts

Leave a Reply