Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. class Rational(val num:Int, val denum:Int) {
  2.  
  3. def +(that:Rational) =
  4. new Rational(num*that.denum + that.num*denum, denum*that.denum)
  5.  
  6. def *(that:Rational) =
  7. new Rational(num*that.num, denum*that.denum)
  8.  
  9. override def toString = num + "/" + denum
  10.  
  11. def minus (that:Rational) = {
  12. new Rational(num*that.denum - that.num*denum, denum*that.denum)
  13. }
  14.  
  15. def divide(that:Rational) = {
  16. new Rational(num * that.denum, denum * that.num)
  17. }
  18.  
  19. def neg = {
  20. new Rational (num*(-1), denum*(-1))
  21. }
  22.  
  23. def <(that:Rational) = {
  24. if (num*that.denum<that.num*denum)
  25. true
  26. else false
  27. }
  28.  
  29. def ==(that:Rational) = {
  30. if (num*that.denum==that.num*denum) true
  31. else false
  32. }
  33.  
  34.  
  35. def max(that:Rational) = {
  36. if (num*that.denum<that.num*denum)
  37. new Rational (that.num, that.denum)
  38. else
  39. new Rational (num, denum)
  40. }
  41.  
  42. def min(that:Rational) = {
  43. if (num*that.denum<that.num*denum)
  44. new Rational (num, denum)
  45. else
  46. new Rational (that.num, that.denum)
  47. }
  48. }
  49. object Rational {
  50. def apply(n:Int) = new Rational(n, 1)
  51. def apply(n:Int, dn:Int) = new Rational(n, dn)
  52. }
  53.  
  54. object na extends App {
  55. val r1 = Rational(5,2)
  56. val r2 = Rational(8,4)
  57. val r3 = Rational(6,3)
  58.  
  59. val rp = r2 minus r1
  60.  
  61. println(r1 min r2)
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement