Advertisement
calcpage

LACS07a_Rational.java

Jun 12th, 2012
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.30 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac Rational.java
  3.  *  Execution:    java Rational
  4.  *
  5.  *  ADT for nonnegative Rational numbers. Bare-bones implementation.
  6.  *  Cancel common factors, but does not stave off overflow. Does not
  7.  *  support negative fractions.
  8.  *
  9.  *  Invariant: all Rational objects are in reduced form (except
  10.  *  possibly while modifying).
  11.  *
  12.  *  Remarks
  13.  *  --------
  14.  *    - See http://www.cs.princeton.edu/introcs/92symbolic/BigRational.java.html
  15.  *      for a version that supports negative fractions and arbitrary
  16.  *      precision numerators and denominators.
  17.  *
  18.  *  % java Rational
  19.  *  5/6
  20.  *  1
  21.  *  28/51
  22.  *  17/899
  23.  *  0
  24.  *
  25.  *************************************************************************/
  26.  
  27. public class Rational {
  28.     private int num;   // the numerator
  29.     private int den;   // the denominator
  30.  
  31.     // create and initialize a new Rational object
  32.     public Rational(int numerator, int denominator) {
  33.         if (denominator == 0) {
  34.             throw new RuntimeException("Denominator is zero");
  35.         }
  36.         int g = gcd(numerator, denominator);
  37.         num = numerator   / g;
  38.         den = denominator / g;
  39.  
  40.     }
  41.  
  42.     // return string representation of (this)
  43.     public String toString() {
  44.         if (den == 1) { return num + "";        }
  45.         else          { return num + "/" + den; }
  46.     }
  47.  
  48.     // return (this * b)
  49.     public Rational times(Rational b) {
  50.         return new Rational(this.num * b.num, this.den * b.den);
  51.     }
  52.  
  53.  
  54.     // return (this + b)
  55.     public Rational plus(Rational b) {
  56.         int numerator   = (this.num * b.den) + (this.den * b.num);
  57.         int denominator = this.den * b.den;
  58.         return new Rational(numerator, denominator);
  59.     }
  60.  
  61.     // return (1 / this)
  62.     public Rational reciprocal() { return new Rational(den, num);  }
  63.  
  64.     // return (this / b)
  65.     public Rational divides(Rational b) {
  66.         return this.times(b.reciprocal());
  67.     }
  68.  
  69.  
  70.    /*************************************************************************
  71.     *  Helper functions
  72.     *************************************************************************/
  73.  
  74.     // return gcd(m, n)
  75.     private static int gcd(int m, int n) {
  76.         if (0 == n) return m;
  77.         else return gcd(n, m % n);
  78.     }
  79.  
  80.  
  81.    /*************************************************************************
  82.     *  Test client
  83.     *************************************************************************/
  84.  
  85.     public static void main(String[] args) {
  86.         Rational x, y, z;
  87.  
  88.         // 1/2 + 1/3 = 5/6
  89.         x = new Rational(1, 2);
  90.         y = new Rational(1, 3);
  91.         z = x.plus(y);
  92.         System.out.println(z);
  93.  
  94.         // 8/9 + 1/9 = 1
  95.         x = new Rational(8, 9);
  96.         y = new Rational(1, 9);
  97.         z = x.plus(y);
  98.         System.out.println(z);
  99.  
  100.         //  4/17 * 7/3 = 28/51
  101.         x = new Rational(4, 17);
  102.         y = new Rational(7,  3);
  103.         z = x.times(y);
  104.         System.out.println(z);
  105.  
  106.         // 203/16957 * 9299/5887 = 17/899
  107.         x = new Rational(203, 16957);
  108.         y = new Rational(9299, 5887);
  109.         z = x.times(y);
  110.         System.out.println(z);
  111.  
  112.         // 0/6 = 0
  113.         x = new Rational(0, 6);
  114.         System.out.println(x);
  115.  
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement