Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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/dd

Explanation / 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)

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote