KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EDU > oswego > cs > dl > util > concurrent > misc > Fraction


1 /*
2   File: Fraction.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   7Jul1998 dl Create public version
12   11Oct1999 dl add hashCode
13 */

14
15 package EDU.oswego.cs.dl.util.concurrent.misc;
16
17
18 /**
19  * An immutable class representing fractions as pairs of longs.
20  * Fractions are always maintained in reduced form.
21  **/

22 public class Fraction implements Cloneable JavaDoc, Comparable JavaDoc, java.io.Serializable JavaDoc {
23   protected final long numerator_;
24   protected final long denominator_;
25
26   /** Return the numerator **/
27   public final long numerator() { return numerator_; }
28
29   /** Return the denominator **/
30   public final long denominator() { return denominator_; }
31
32   /** Create a Fraction equal in value to num / den **/
33   public Fraction(long num, long den) {
34     // normalize while constructing
35
boolean numNonnegative = (num >= 0);
36     boolean denNonnegative = (den >= 0);
37     long a = numNonnegative? num : -num;
38     long b = denNonnegative? den : -den;
39     long g = gcd(a, b);
40     numerator_ = (numNonnegative == denNonnegative)? (a / g) : (-a / g);
41     denominator_ = b / g;
42   }
43
44   /** Create a fraction with the same value as Fraction f **/
45   public Fraction(Fraction f) {
46     numerator_ = f.numerator();
47     denominator_ = f.denominator();
48   }
49
50   public String JavaDoc toString() {
51     if (denominator() == 1)
52       return "" + numerator();
53     else
54       return numerator() + "/" + denominator();
55   }
56
57   public Object JavaDoc clone() { return new Fraction(this); }
58
59   /** Return the value of the Fraction as a double **/
60   public double asDouble() {
61     return ((double)(numerator())) / ((double)(denominator()));
62   }
63
64   /**
65    * Compute the nonnegative greatest common divisor of a and b.
66    * (This is needed for normalizing Fractions, but can be
67    * useful on its own.)
68    **/

69   public static long gcd(long a, long b) {
70     long x;
71     long y;
72
73     if (a < 0) a = -a;
74     if (b < 0) b = -b;
75
76     if (a >= b) { x = a; y = b; }
77     else { x = b; y = a; }
78
79     while (y != 0) {
80       long t = x % y;
81       x = y;
82       y = t;
83     }
84     return x;
85   }
86
87   /** return a Fraction representing the negated value of this Fraction **/
88   public Fraction negative() {
89     long an = numerator();
90     long ad = denominator();
91     return new Fraction(-an, ad);
92   }
93
94   /** return a Fraction representing 1 / this Fraction **/
95   public Fraction inverse() {
96     long an = numerator();
97     long ad = denominator();
98     return new Fraction(ad, an);
99   }
100
101
102   /** return a Fraction representing this Fraction plus b **/
103   public Fraction plus(Fraction b) {
104     long an = numerator();
105     long ad = denominator();
106     long bn = b.numerator();
107     long bd = b.denominator();
108     return new Fraction(an*bd+bn*ad, ad*bd);
109   }
110
111   /** return a Fraction representing this Fraction plus n **/
112   public Fraction plus(long n) {
113     long an = numerator();
114     long ad = denominator();
115     long bn = n;
116     long bd = 1;
117     return new Fraction(an*bd+bn*ad, ad*bd);
118   }
119
120   /** return a Fraction representing this Fraction minus b **/
121   public Fraction minus(Fraction b) {
122     long an = numerator();
123     long ad = denominator();
124     long bn = b.numerator();
125     long bd = b.denominator();
126     return new Fraction(an*bd-bn*ad, ad*bd);
127   }
128
129   /** return a Fraction representing this Fraction minus n **/
130   public Fraction minus(long n) {
131     long an = numerator();
132     long ad = denominator();
133     long bn = n;
134     long bd = 1;
135     return new Fraction(an*bd-bn*ad, ad*bd);
136   }
137
138
139   /** return a Fraction representing this Fraction times b **/
140   public Fraction times(Fraction b) {
141     long an = numerator();
142     long ad = denominator();
143     long bn = b.numerator();
144     long bd = b.denominator();
145     return new Fraction(an*bn, ad*bd);
146   }
147
148   /** return a Fraction representing this Fraction times n **/
149   public Fraction times(long n) {
150     long an = numerator();
151     long ad = denominator();
152     long bn = n;
153     long bd = 1;
154     return new Fraction(an*bn, ad*bd);
155   }
156
157   /** return a Fraction representing this Fraction divided by b **/
158   public Fraction dividedBy(Fraction b) {
159     long an = numerator();
160     long ad = denominator();
161     long bn = b.numerator();
162     long bd = b.denominator();
163     return new Fraction(an*bd, ad*bn);
164   }
165
166   /** return a Fraction representing this Fraction divided by n **/
167   public Fraction dividedBy(long n) {
168     long an = numerator();
169     long ad = denominator();
170     long bn = n;
171     long bd = 1;
172     return new Fraction(an*bd, ad*bn);
173   }
174
175   /** return a number less, equal, or greater than zero
176    * reflecting whether this Fraction is less, equal or greater than
177    * the value of Fraction other.
178    **/

179   public int compareTo(Object JavaDoc other) {
180     Fraction b = (Fraction)(other);
181     long an = numerator();
182     long ad = denominator();
183     long bn = b.numerator();
184     long bd = b.denominator();
185     long l = an*bd;
186     long r = bn*ad;
187     return (l < r)? -1 : ((l == r)? 0: 1);
188   }
189
190   /** return a number less, equal, or greater than zero
191    * reflecting whether this Fraction is less, equal or greater than n.
192    **/

193
194   public int compareTo(long n) {
195     long an = numerator();
196     long ad = denominator();
197     long bn = n;
198     long bd = 1;
199     long l = an*bd;
200     long r = bn*ad;
201     return (l < r)? -1 : ((l == r)? 0: 1);
202   }
203
204   public boolean equals(Object JavaDoc other) {
205     return compareTo((Fraction)other) == 0;
206   }
207
208   public boolean equals(long n) {
209     return compareTo(n) == 0;
210   }
211
212   public int hashCode() {
213     return (int) (numerator_ ^ denominator_);
214   }
215
216 }
217
Popular Tags