Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Use
- def mulRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- return Rational(getNumer(x)*getNumer(y), getDenom(x)*getDenom(y)) #x and y are abstract data
- def addRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- nx, dx = getNumer(x), getDenom(x)
- ny, dy = getNumer(y), getDenom(y)
- return Rational(nx * dy + ny * dx, dx * dy)
- def eqRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- return getNumer(x) * getDenom(y) == getNumer(y) * getDenom(x)
- def toString(x):
- """Violate abstraction by using other than constructor and selectors"""
- return '{0}/{1}'.format(getNumer(x), getDenom(x))
- #Representation
- # Representation is provided by constructors and selectors using tuples
- #Constructor
- from fractions import gcd
- def Rational(n, d):
- """Construct a rational number x that represents n/d."""
- g = gcd(n, d)
- return (n // g, d // g) #this is concrete representation of a rational number
- #Selector
- from operator import getitem
- def getNumer(x):
- """Return the numerator of rational number x."""
- return getitem(x, 0)
- #Selector
- def getDenom(x):
- """Return the denominator of rational number x."""
- return getitem(x, 1)
- #Use
- def mulRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- return pair(getitem_pair(x,0) * getitem_pair(y,0), getitem_pair(x,1) * getitem_pair(y,1))
- def addRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- nx, dx = getitem_pair(x,0), getitem_pair(x,1)
- ny, dy = getitem_pair(y,0), getitem_pair(y,1)
- return pair(nx * dy + ny * dx, dx * dy)
- def eqRational(x, y):
- """Violate abstraction by using other than constructor and selectors"""
- return getitem_pair(x,0) * getitem_pair(y,1) == getitem_pair(y,0) * getitem_pair(x,1)
- def toString(x):
- """Violate abstraction by using other than constructor and selectors"""
- return '{0}/{1}'.format(getitem_pair(x,0), getitem_pair(x,1))
- #Representation
- #Representation is provided by constructors and selectors using higher order functions
- #Constructor
- def pair(x, y):
- from fractions import gcd
- g = gcd(x, y)
- """Return a functional pair"""
- def dispatch(m):
- if m == 0:
- return x // g
- elif m == 1:
- return y // g
- return dispatch
- #Selector
- def getitem_pair(p, i):
- """Return the element at index of pair p"""
- return p(i)
- public class Rational{
- /* Representation - starts*/
- private int[] tuple;
- public Rational(int n, int d){
- this.tuple = new int[2];
- int g = gcd(n, d);
- this.tuple[0] = n / g;
- this.tuple[1] = d / g;
- }
- private int getNumer(){
- return this.tuple[0];
- }
- private int getDenom(){
- return this.tuple[1];
- }
- /* Representation - ends*/
- /*
- * helper function
- */
- private static int gcd(int n, int d){
- if (d == 0)
- return n;
- else
- return gcd(d, n % d);
- }
- /*
- * Use - starts
- */
- public Rational mulRational(Rational x, Rational y){
- return new Rational(x.getNumer()*y.getNumer(), x.getDenom()*y.getDenom());
- }
- public Rational addRational(Rational x, Rational y){
- return new Rational(x.getNumer() * y.getDenom() + y.getNumer() * x.getDenom(), x.getDenom() * y.getDenom());
- }
- /*
- * Logical equality
- *
- */
- @Override
- public boolean equals(Object obj) {
- return this.getNumer() * ((Rational)obj).getDenom() == ((Rational)obj).getNumer() * this.getDenom();
- }
- @Override
- public int hashCode() {
- int result = 17;
- result = 31 * result + this.getNumer();
- result = 31 * result + this.getDenom();
- return result;
- }
- @Override
- public final String toString() {
- return this.getNumer() + "/" + this.getDenom();
- }
- /*
- * Use - ends
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement