Skip to content

Instantly share code, notes, and snippets.

@dgadiraju
Created May 10, 2017 06:48
Show Gist options
  • Select an option

  • Save dgadiraju/0abaacb41474edc611b8a51fc18de138 to your computer and use it in GitHub Desktop.

Select an option

Save dgadiraju/0abaacb41474edc611b8a51fc18de138 to your computer and use it in GitHub Desktop.
/**
* Created by itversity on 10/05/17.
*/
class Fraction(val n: Int, val d: Int) {
override def toString = n + "/" + d
def result = n/d.toDouble
def +(f: Fraction) = {
new Fraction(((n*f.d) + (f.n*d)), (d * f.d))
}
}
object Fraction {
def main(args: Array[String]): Unit = {
val f = new Fraction(2, 4)
println(f)
println(f.result)
val s = new Fraction(5, 3)
println(s)
println(s.result)
val r = f + s
println(r)
println(r.result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment