1 14 15 package EDU.oswego.cs.dl.util.concurrent.misc; 16 17 18 22 public class Fraction implements Cloneable , Comparable , java.io.Serializable { 23 protected final long numerator_; 24 protected final long denominator_; 25 26 27 public final long numerator() { return numerator_; } 28 29 30 public final long denominator() { return denominator_; } 31 32 33 public Fraction(long num, long den) { 34 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 45 public Fraction(Fraction f) { 46 numerator_ = f.numerator(); 47 denominator_ = f.denominator(); 48 } 49 50 public String toString() { 51 if (denominator() == 1) 52 return "" + numerator(); 53 else 54 return numerator() + "/" + denominator(); 55 } 56 57 public Object clone() { return new Fraction(this); } 58 59 60 public double asDouble() { 61 return ((double)(numerator())) / ((double)(denominator())); 62 } 63 64 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 88 public Fraction negative() { 89 long an = numerator(); 90 long ad = denominator(); 91 return new Fraction(-an, ad); 92 } 93 94 95 public Fraction inverse() { 96 long an = numerator(); 97 long ad = denominator(); 98 return new Fraction(ad, an); 99 } 100 101 102 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 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 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 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 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 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 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 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 179 public int compareTo(Object 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 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 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 |