Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rational(val num:Int, val denum:Int) {
- def +(that:Rational) =
- new Rational(num*that.denum + that.num*denum, denum*that.denum)
- def *(that:Rational) =
- new Rational(num*that.num, denum*that.denum)
- override def toString = num + "/" + denum
- def minus (that:Rational) = {
- new Rational(num*that.denum - that.num*denum, denum*that.denum)
- }
- def divide(that:Rational) = {
- new Rational(num * that.denum, denum * that.num)
- }
- def neg = {
- new Rational (num*(-1), denum*(-1))
- }
- def <(that:Rational) = {
- if (num*that.denum<that.num*denum)
- true
- else false
- }
- def ==(that:Rational) = {
- if (num*that.denum==that.num*denum) true
- else false
- }
- def max(that:Rational) = {
- if (num*that.denum<that.num*denum)
- new Rational (that.num, that.denum)
- else
- new Rational (num, denum)
- }
- def min(that:Rational) = {
- if (num*that.denum<that.num*denum)
- new Rational (num, denum)
- else
- new Rational (that.num, that.denum)
- }
- }
- object Rational {
- def apply(n:Int) = new Rational(n, 1)
- def apply(n:Int, dn:Int) = new Rational(n, dn)
- }
- object na extends App {
- val r1 = Rational(5,2)
- val r2 = Rational(8,4)
- val r3 = Rational(6,3)
- val rp = r2 minus r1
- println(r1 min r2)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement