Lesson 26: Calling methods from another method of the same class – kotlin

Lesson 26: Calling methods from another method of the same class – kotlin

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

So far all the problems raised have called the methods from where we define an object of that class, for example:

    Val persona1 = Person ("Juan", 12)
    Print ()
    Person1.MajorAge ()

We use the syntax:

[Name of the object] [method name]

That is, we precede the name of the method with the name of the object and the operator point.

Now what happens if we want to call within the class another method that belongs to the same class, the syntax is as follows:

[Method name]

O in long form:

This [method name]

It is important to keep in mind that this can only be done when we are within the same class.

Problem 1

Putting a class Operations that in one method requests the loading of 2 integers and then call two other methods that calculate their sum and product.

Project117 – Principal.kt

class operations {
    var value1: Int = 0
    var value2: Int = 0

    fun load () {
        print ("Enter first value:")
        value1 = readLine () !!. toInt ()
        print ("Enter second value:")
        value2 = readLine () !!. toInt ()
        add()
        subtract()
    }

    fun sumar () {
        val sum = value1 + value2
        println ("The sum of $value1 and $value2 is $sum")
    }

    funprize () {
        val subtraction = value1 - value2
        println ("The subtraction of $value1 and $value2 is $subtraction")
    }
}

fun main (parameter: Array <String>) {
    val operations1 = Operations ()
    operations1.load ()
}

Our method load in addition to loading the two integers in the properties proceeds to call the methods that calculate the addition and subtraction of the two values ​​entered.

The call of the methods of the same class is done indicating the name of the method.

From where we define an object of the class Operations we call its methods preceding the name of the object:

    Val operations1 = Operations ()
    Operations1.load ()

Proposed problem

  • Declare a class called Children. Define within it an arrangement to store the ages of 5 people.
    Define a load method where you enter the age arrangement and call two other methods that print the oldest and the average age.
Project118

class children {
    val ages = IntArray (5)

    fun load () {
        for (i in ages.indices) {
            print ("Enter age:")
            ages [i] = readLine () !!. toInt ()
        }
        majorAge()
        average()
    }

     fun majorAge () {
        var major = ages [0]
        for (i in ages.indices)
            if (ages [i]> major)
                major = ages [i]
        println ("The oldest child is $major")
    }

    fun average () {
        var sum = 0
        for (i in ages.indices)
            sum += ages [i]
        val average = sum / ages.size
        println ("The average age of children is $average")
    }
}

fun main (Parameters: Array <String>) {
    val o = children ()
    o.load ()
}


About The Author

Related posts

Leave a Reply