Home > Back-end >  How to multiply and sumup fractions in kotlin
How to multiply and sumup fractions in kotlin

Time:10-16

i'm new to kotlin and i'm having a trouble multiplying same parameters of two objects which are in the same class.

  class Fraction (private val numerator:Int, denumerator:Int)

i have a class called fraction and 2 objects.

    var f1 = Fraction(1,6)
    var f2 = Fraction(3,6)

i want to create a function which multiplies first fraction on the second fraction. also i have to create a function which adds up these two fractions.

can someone help me?

CodePudding user response:

You can use operator overloading here to support plus and times operation.

data class Fraction(val numerator: Int, val denominator: Int) {
    operator fun plus(other: Fraction): Fraction {
        return Fraction(
            numerator = this.numerator * other.denominator   this.denominator * other.numerator,
            denominator = this.denominator * other.denominator
        )
    }

    operator fun times(other: Fraction): Fraction {
        return Fraction(
            numerator = this.numerator * other.numerator,
            denominator = this.denominator * other.denominator
        )
    }
}

Usage:

val f1 = Fraction(1, 2)
val f2 = Fraction(3, 5)
println(f1   f2)
println(f1 * f2)

Output:

Fraction(numerator=11, denominator=10)
Fraction(numerator=3, denominator=10)

Note that the resultant fraction won't be a reduced one. If you want the fraction in its lowest terms, you will have to divide numerator and denominator by their gcd. The final code will be:

data class Fraction(val numerator: Int, val denominator: Int) {
    operator fun plus(other: Fraction): Fraction {
        return Fraction(
            numerator = this.numerator * other.denominator   this.denominator * other.numerator,
            denominator = this.denominator * other.denominator
        ).reduced()
    }

    operator fun times(other: Fraction): Fraction {
        return Fraction(
            numerator = this.numerator * other.numerator,
            denominator = this.denominator * other.denominator
        ).reduced()
    }

    fun reduced(): Fraction {
        val gcd = gcd(numerator, denominator)
        return Fraction(numerator / gcd, denominator / gcd)
    }
}

fun gcd(n1: Int, n2: Int): Int {
    return if (n2 != 0) gcd(n2, n1 % n2) else n1
}

CodePudding user response:

// Use data class instead class because the purpose of class is only hold data
data class Fraction(var numerator: Int, var denumerator: Int)

fun multiplyFraction(f1: Fraction, f2: Fraction) : Fraction {
    val fractionResult = Fraction(
        (f1.numerator * f2.numerator),
        (f1.denumerator * f2.denumerator)
    )
    return fractionResult
}

fun addUpFraction(f1: Fraction, f2: Fraction) : Fraction {
    lateinit var fractionResult: Fraction
    // If f1 e f2 denumerators are equal
    if (f1.denumerator === f2.denumerator) {
        fractionResult = Fraction(
            (f1.numerator   f2.numerator),
            f1.denumerator
        )
    } else { // If not equal
        fractionResult = Fraction(
            ((f1.numerator * f2.denumerator)   (f2.numerator * f1.denumerator)),
            (f1.denumerator * f2.denumerator)
        )
    }
    return fractionResult
}

fun main() {
    // Fractions with denumerators equals
    val f1 = Fraction(1, 5)
    val f2 = Fraction( 3, 5)
    var f3 = addUpFraction(f1, f2)
    println("${f1.numerator}/${f1.denumerator}   ${f2.numerator}/${f2.denumerator} = "  
            "${f3.numerator}/${f3.denumerator}")
    f3 = multiplyFraction(f1, f2)
    println("${f1.numerator}/${f1.denumerator} * ${f2.numerator}/${f2.denumerator} = "  
            "${f3.numerator}/${f3.denumerator}")
    // Fractions with denumerators not equals
    f1.denumerator = 3
    f3 = addUpFraction(f1, f2)
    println("${f1.numerator}/${f1.denumerator}   ${f2.numerator}/${f2.denumerator} = "  
            "${f3.numerator}/${f3.denumerator}")
    f3 = multiplyFraction(f1, f2)
    println("${f1.numerator}/${f1.denumerator} * ${f2.numerator}/${f2.denumerator} = "  
            "${f3.numerator}/${f3.denumerator}")
}

References

  1. How to Add Fractions
  2. How to Multiply Fractions
  3. Data Classes
  • Related