File tree 2 files changed +15
-2
lines changed
main/scala/org/codecraftlabs/training
test/scala/org/codecraftlabs/training
2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,14 @@ class Rational (n: Int, d: Int) {
13
13
14
14
override def toString : String = numer + " /" + denom
15
15
16
- def add (that : Rational ): Rational = {
16
+ def + (that : Rational ): Rational = {
17
17
new Rational (numer * that.denom + that.numer * denom, denom * that.denom)
18
18
}
19
19
20
+ def * (that : Rational ): Rational = {
21
+ new Rational (numer * that.denom, denom * that.numer)
22
+ }
23
+
20
24
def lessThan (that : Rational ): Boolean = {
21
25
this .numer * that.denom < that.numer * this .denom
22
26
}
Original file line number Diff line number Diff line change @@ -8,11 +8,20 @@ class RationalSpec extends AnyFlatSpec with Matchers {
8
8
val r1 = new Rational (1 , 2 )
9
9
val r2 = new Rational (2 , 3 )
10
10
11
- val result = r1.add(r2)
11
+ val result = r1 + r2
12
12
result.numer shouldEqual 7
13
13
result.denom shouldEqual 6
14
14
}
15
15
16
+ " 1/3 * 1/6" should " result in 2" in {
17
+ val r1 = new Rational (1 , 3 )
18
+ val r2 = new Rational (1 , 6 )
19
+
20
+ val result = r1 * r2
21
+ result.numer shouldEqual 2
22
+ result.denom shouldEqual 1
23
+ }
24
+
16
25
" 1/3 lessThan 1/2" should " return true" in {
17
26
val r1 = new Rational (1 , 3 )
18
27
val r2 = new Rational (1 , 2 )
You can’t perform that action at this time.
0 commit comments