Shaho Amini
Shaho Amini

Reputation: 400

Rational code in scala

/* want to read the rational code which compute ((1/2)+(2/3)). I finde this code but I have one question about that*/

object Rationals
{
     val x= new Rational(1, 2) // 1/2

     x.numer // *
     x.denom // **
       /*  * and ** are my questions. why I have to use them?   */

     val y = new Rational(2, 3) // 2/3
     x.add(y)

      /* my result most be equal to 7/6 */
}


class Rational (x : Int, y : Int)
{
     def numer= x
     def denom= y

     def add (that : Rational) =
     new Rational (
     numer * that.denom + that.numer * denom,   /* 1*3 + 2*2 */
     denom * that.denom) /* 2*2 */

    override def toString = numer + "/" + denom   /* 7/6 */

 }

Upvotes: 0

Views: 1105

Answers (1)

dhg
dhg

Reputation: 52701

The lines:

     x.numer // *
     x.denom // **

aren't doing anything. They are getting computed but not used; and they don't have side-effects.

Upvotes: 1

Related Questions