Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #Use
  2. def mulRational(x, y):
  3. """Violate abstraction by using other than constructor and selectors"""
  4. return Rational(getNumer(x)*getNumer(y), getDenom(x)*getDenom(y)) #x and y are abstract data
  5.  
  6. def addRational(x, y):
  7. """Violate abstraction by using other than constructor and selectors"""
  8. nx, dx = getNumer(x), getDenom(x)
  9. ny, dy = getNumer(y), getDenom(y)
  10. return Rational(nx * dy + ny * dx, dx * dy)
  11.  
  12. def eqRational(x, y):
  13. """Violate abstraction by using other than constructor and selectors"""
  14. return getNumer(x) * getDenom(y) == getNumer(y) * getDenom(x)
  15.  
  16. def toString(x):
  17. """Violate abstraction by using other than constructor and selectors"""
  18. return '{0}/{1}'.format(getNumer(x), getDenom(x))
  19.  
  20. #Representation
  21. # Representation is provided by constructors and selectors using tuples
  22.  
  23. #Constructor
  24. from fractions import gcd
  25. def Rational(n, d):
  26. """Construct a rational number x that represents n/d."""
  27. g = gcd(n, d)
  28. return (n // g, d // g) #this is concrete representation of a rational number
  29.  
  30.  
  31. #Selector
  32. from operator import getitem
  33. def getNumer(x):
  34. """Return the numerator of rational number x."""
  35. return getitem(x, 0)
  36.  
  37. #Selector
  38. def getDenom(x):
  39. """Return the denominator of rational number x."""
  40. return getitem(x, 1)
  41.  
  42. #Use
  43. def mulRational(x, y):
  44. """Violate abstraction by using other than constructor and selectors"""
  45. return pair(getitem_pair(x,0) * getitem_pair(y,0), getitem_pair(x,1) * getitem_pair(y,1))
  46.  
  47. def addRational(x, y):
  48. """Violate abstraction by using other than constructor and selectors"""
  49. nx, dx = getitem_pair(x,0), getitem_pair(x,1)
  50. ny, dy = getitem_pair(y,0), getitem_pair(y,1)
  51. return pair(nx * dy + ny * dx, dx * dy)
  52.  
  53. def eqRational(x, y):
  54. """Violate abstraction by using other than constructor and selectors"""
  55. return getitem_pair(x,0) * getitem_pair(y,1) == getitem_pair(y,0) * getitem_pair(x,1)
  56.  
  57. def toString(x):
  58. """Violate abstraction by using other than constructor and selectors"""
  59. return '{0}/{1}'.format(getitem_pair(x,0), getitem_pair(x,1))
  60.  
  61.  
  62. #Representation
  63. #Representation is provided by constructors and selectors using higher order functions
  64.  
  65. #Constructor
  66. def pair(x, y):
  67. from fractions import gcd
  68. g = gcd(x, y)
  69. """Return a functional pair"""
  70. def dispatch(m):
  71. if m == 0:
  72. return x // g
  73.  
  74. elif m == 1:
  75. return y // g
  76. return dispatch
  77.  
  78. #Selector
  79. def getitem_pair(p, i):
  80. """Return the element at index of pair p"""
  81. return p(i)
  82.  
  83. public class Rational{
  84.  
  85. /* Representation - starts*/
  86.  
  87. private int[] tuple;
  88.  
  89.  
  90.  
  91. public Rational(int n, int d){
  92. this.tuple = new int[2];
  93. int g = gcd(n, d);
  94. this.tuple[0] = n / g;
  95. this.tuple[1] = d / g;
  96.  
  97. }
  98.  
  99. private int getNumer(){
  100. return this.tuple[0];
  101. }
  102.  
  103. private int getDenom(){
  104. return this.tuple[1];
  105. }
  106. /* Representation - ends*/
  107.  
  108. /*
  109. * helper function
  110. */
  111. private static int gcd(int n, int d){
  112. if (d == 0)
  113. return n;
  114. else
  115. return gcd(d, n % d);
  116. }
  117.  
  118.  
  119. /*
  120. * Use - starts
  121. */
  122. public Rational mulRational(Rational x, Rational y){
  123. return new Rational(x.getNumer()*y.getNumer(), x.getDenom()*y.getDenom());
  124. }
  125.  
  126.  
  127. public Rational addRational(Rational x, Rational y){
  128. return new Rational(x.getNumer() * y.getDenom() + y.getNumer() * x.getDenom(), x.getDenom() * y.getDenom());
  129.  
  130. }
  131.  
  132. /*
  133. * Logical equality
  134. *
  135. */
  136. @Override
  137. public boolean equals(Object obj) {
  138.  
  139. return this.getNumer() * ((Rational)obj).getDenom() == ((Rational)obj).getNumer() * this.getDenom();
  140. }
  141.  
  142.  
  143. @Override
  144. public int hashCode() {
  145. int result = 17;
  146. result = 31 * result + this.getNumer();
  147. result = 31 * result + this.getDenom();
  148. return result;
  149. }
  150.  
  151. @Override
  152. public final String toString() {
  153. return this.getNumer() + "/" + this.getDenom();
  154. }
  155.  
  156. /*
  157. * Use - ends
  158. */
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement