Skip to content

Instantly share code, notes, and snippets.

@HDBandit
Created July 31, 2016 12:22
Show Gist options
  • Save HDBandit/8a9962e5a76465fa768ee13febb50f28 to your computer and use it in GitHub Desktop.
Save HDBandit/8a9962e5a76465fa768ee13febb50f28 to your computer and use it in GitHub Desktop.
class Rational(n: Int, d: Int) {
require(d != 0, "Denominator cannot be 0")
println("My rational is created")
val num = n
val denom = d
override def toString = s"$n/$d"
def +(that: Rational): Rational = {
new Rational(
n * that.denom + that.num * d,
d * that.denom
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment