Advertisement
rplantiko

Rational Numbers in Groovy

Oct 15th, 2011
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 6.39 KB | None | 0 0
  1. //  Class RationalNumber Author: Lewis/Loftus
  2. //  http://www.cs.dartmouth.edu/~cs5/examples/chap06/RationalNumber.java
  3. //
  4. //  Represents one rational number with a numerator and denominator.
  5. //
  6. // --> Slightly adapted by R. Plantiko,
  7. // It remains more or less a POJO
  8. // But the method naming is important in Groovy:
  9. //   x * y will automatically be bound to plus(x,y) if plus() has a signature that fits
  10. // The same for minus, multiply, div.
  11. // And, as in Java, equals() and toString() have special meaning
  12. // Handling of denominator 0: Computations can be continued, but any div by 0 gives a
  13. // normalized representation NaN = (1,0) which makes it identifiable
  14.  
  15. public class RationalNumber
  16. {
  17.    private int numerator, denominator;
  18.    
  19.    public static final RationalNumber DIV_BY_ZERO = new RationalNumber(1,0);
  20.    
  21.    //-----------------------------------------------------------------
  22.    //  Constructor: Sets up the rational number by ensuring a nonzero
  23.    //  denominator and making only the numerator signed.
  24.    //-----------------------------------------------------------------
  25.    public RationalNumber (int numer, int denom)
  26.    {
  27.    
  28. // Normalize all "Div-By-Zero" fractions
  29.       if (denom == 0) {
  30.          numerator = 1;
  31.          denominator = 0;
  32.          }
  33.  
  34.       // Make the numerator "store" the sign
  35.       else {
  36.        
  37.          if (denom < 0) {
  38.            numer = numer * -1;
  39.            denom = denom * -1;
  40.            }
  41.  
  42.          numerator = numer;
  43.          denominator = denom;
  44.        
  45.          reduce();
  46.  
  47.        }
  48.  
  49.   }
  50.    
  51.    //-----------------------------------------------------------------
  52.    //  Returns the numerator of this rational number.
  53.    //-----------------------------------------------------------------
  54.    public int getNumerator ()
  55.    {
  56.       return numerator;
  57.    }
  58.  
  59.    //-----------------------------------------------------------------
  60.    //  Returns the denominator of this rational number.
  61.    //-----------------------------------------------------------------
  62.    public int getDenominator ()
  63.    {
  64.       return denominator;
  65.    }
  66.  
  67.    //-----------------------------------------------------------------
  68.    //  Returns the reciprocal of this rational number.
  69.    //-----------------------------------------------------------------
  70.    public RationalNumber reciprocal ()
  71.    {
  72.       return new RationalNumber (denominator, numerator);
  73.    }
  74.  
  75.    //-----------------------------------------------------------------
  76.    //  Adds this rational number to the one passed as a parameter.
  77.    //  A common denominator is found by multiplying the individual
  78.    //  denominators.
  79.    //-----------------------------------------------------------------
  80.    public RationalNumber plus(RationalNumber op2)
  81.    {
  82.       int commonDenominator = denominator * op2.getDenominator();
  83.       int numerator1 = numerator * op2.getDenominator();
  84.       int numerator2 = op2.getNumerator() * denominator;
  85.       int sum = numerator1 + numerator2;
  86.  
  87.       return new RationalNumber (sum, commonDenominator);
  88.    }
  89.  
  90.    //-----------------------------------------------------------------
  91.    //  Subtracts the rational number passed as a parameter from this
  92.    //  rational number.
  93.    //-----------------------------------------------------------------
  94.    public RationalNumber minus(RationalNumber op2)
  95.    {
  96.       int commonDenominator = denominator * op2.getDenominator();
  97.       int numerator1 = numerator * op2.getDenominator();
  98.       int numerator2 = op2.getNumerator() * denominator;
  99.       int difference = numerator1 - numerator2;
  100.  
  101.       return new RationalNumber (difference, commonDenominator);
  102.    }
  103.  
  104.    //-----------------------------------------------------------------
  105.    //  Multiplies this rational number by the one passed as a
  106.    //  parameter.
  107.    //-----------------------------------------------------------------
  108.    public RationalNumber multiply (RationalNumber op2)
  109.    {
  110.       int numer = numerator * op2.getNumerator();
  111.       int denom = denominator * op2.getDenominator();
  112.  
  113.       return new RationalNumber (numer, denom);
  114.    }
  115.  
  116.    //-----------------------------------------------------------------
  117.    //  Divides this rational number by the one passed as a parameter
  118.    //  by multiplying by the reciprocal of the second rational.
  119.    //-----------------------------------------------------------------
  120.    public RationalNumber div(RationalNumber op2)
  121.    {
  122.       return multiply (op2.reciprocal());
  123.    }
  124.  
  125.    //-----------------------------------------------------------------
  126.    //  Determines if this rational number is equal to the one passed
  127.    //  as a parameter.  Assumes they are both reduced.
  128.    //-----------------------------------------------------------------
  129.    public boolean equals (RationalNumber op2)
  130.    {
  131.       return ( numerator == op2.getNumerator() &&
  132.                denominator == op2.getDenominator() );
  133.    }
  134.  
  135.    //-----------------------------------------------------------------
  136.    //  Returns this rational number as a string.
  137.    //-----------------------------------------------------------------
  138.    public String toString ()
  139.    {
  140.       String result;
  141.  
  142.       if (numerator == 0)
  143.          result = "0";
  144.       else
  145.          if (denominator == 1)
  146.             result = numerator + "";
  147.          else if (denominator == 0)
  148.             result = "NaN"
  149.          else
  150.             result = numerator + "/" + denominator;
  151.    
  152.       return result;
  153.    }
  154.  
  155.    //-----------------------------------------------------------------
  156.    //  Reduces this rational number by dividing both the numerator
  157.    //  and the denominator by their greatest common divisor.
  158.    //-----------------------------------------------------------------
  159.    private void reduce ()
  160.    {
  161.       if (numerator != 0)
  162.       {
  163.          int common = gcd (Math.abs(numerator), denominator);
  164.  
  165.          numerator = numerator / common;
  166.          denominator = denominator / common;
  167.       }
  168.    }
  169.  
  170.    //-----------------------------------------------------------------
  171.    //  Computes and returns the greatest common divisor of the two
  172.    //  positive parameters. Uses Euclid's algorithm.
  173.    //-----------------------------------------------------------------
  174.    private int gcd (int num1, int num2)
  175.    {
  176.       while (num1 != num2)
  177.          if (num1 > num2)
  178.             num1 = num1 - num2;
  179.          else
  180.             num2 = num2 - num1;
  181.  
  182.       return num1;
  183.    }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement