File tree 2 files changed +28
-0
lines changed
main/scala/org/codecraftlabs/training
test/scala/org/codecraftlabs/training
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org .codecraftlabs .training
2
+
3
+ class Rational (n : Int , d : Int ) {
4
+ require(d != 0 )
5
+
6
+ val numer : Int = n
7
+ val denom : Int = d
8
+
9
+ override def toString : String = numer + " /" + denom
10
+
11
+ def add (that : Rational ): Rational =
12
+ new Rational (numer * that.denom + that.numer * denom, denom * that.denom)
13
+ }
Original file line number Diff line number Diff line change
1
+ package org .codecraftlabs .training
2
+
3
+ import org .scalatest .flatspec .AnyFlatSpec
4
+ import org .scalatest .matchers .should .Matchers
5
+
6
+ class RationalSpec extends AnyFlatSpec with Matchers {
7
+ " 1/2 + 2/3" should " 7/6" in {
8
+ val r1 = new Rational (1 , 2 )
9
+ val r2 = new Rational (2 , 3 )
10
+
11
+ val result = r1.add(r2)
12
+ result.numer shouldEqual 7
13
+ result.denom shouldEqual 6
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments