Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rational(n: Int, d: Int) {
- require(d != 0)
- val numer = n
- val denum = d
- def this(n: Int) = this(n, 1)
- def + (r2: Rational): Rational =
- new Rational(
- numer * r2.denum + r2.numer * denum,
- denum * r2.denum
- )
- override def toString = numer + "/" + denum
- }
- val r1 = new Rational(2, 3)
- val r2 = new Rational(3)
- val r3 = r1 + r2
- println(r1 + " + " + r2 + " = " + r3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement