added rationals
This commit is contained in:
21
src/Lesson3/Rational.sc
Normal file
21
src/Lesson3/Rational.sc
Normal file
@@ -0,0 +1,21 @@
|
||||
class Rational(n: Int, d: Int) {
|
||||
def num = n
|
||||
def denom = d
|
||||
}
|
||||
|
||||
def add(x: Rational, y: Rational): Rational =
|
||||
new Rational(
|
||||
x.num * y.denom + x.denom * y.num,
|
||||
x.denom * y.denom
|
||||
)
|
||||
|
||||
def stringVersion(x: Rational) = x.num + "/" + x.denom
|
||||
|
||||
val r1 = new Rational(1, 2)
|
||||
r1.num
|
||||
r1.denom
|
||||
|
||||
val r2 = new Rational(3, 4)
|
||||
|
||||
val r3 = add(r1, r2)
|
||||
stringVersion(r3)
|
||||
Reference in New Issue
Block a user