We would like you to implement the methods in the class Rational that is suppose
ID: 3743567 • Letter: W
Question
We would like you to implement the methods in the class Rational that is supposed to represent fractionswhere the numerator and denominator are integers n, d. Note that it is important to ensure that d f 0 Implement the missing methods below You should read this blog post about how to convert between numeric types https://alvinalexande meric-types- in-scala-int-long-float-double ow- class Rational(val n: Int, val d: Int) //Implement a pretty printer override def toString(): String = { s"$n/$d" def isValid(): Boolean { var ret = true retfalse return ret def toDouble(): Double val dn n.toDouble val dd d.toDouble return dn/ddExplanation / Answer
def rationalEquals(r: Rational): Boolean = {
var ret = false
if(n == r.n)
{
if(d == r.d)
{
ret = true
}
}
return ret
}
def + (r: Rational): Rational = {
new Rational(n * r.d + r.n * d , d * r.d)
}
def - (r: Rational): Rational = {
new Rational(n * r.d - r.n * d , d * r.d)
}
def * (r: Rational): Rational = {
new Rational(n * r.n , d * r.d)
}
def / (r: Rational): Rational = {
new Rational(n * r.d , d * r.n)
}