Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.math.RoundingMode;
  4.  
  5. public final class Fraction implements Comparable<Fraction>
  6. {
  7. private BigInteger numerator;
  8. private BigInteger denominator;
  9.  
  10. Fraction(int num)
  11. {
  12. this.initializeFraction(BigInteger.valueOf(num), BigInteger.ONE);
  13. }
  14.  
  15. Fraction(BigInteger num, BigInteger den)
  16. {
  17. this.initializeFraction(num, den);
  18. }
  19.  
  20. Fraction(String fraction)
  21. {
  22. String[] tokens = fraction.split("/");
  23.  
  24. this.initializeFraction(
  25. new BigInteger(tokens[0]),
  26. new BigInteger(tokens[1])
  27. );
  28. }
  29.  
  30. private void initializeFraction(BigInteger num, BigInteger den)
  31. {
  32. if (den.signum() == 0)
  33. throw new IllegalArgumentException("Zero denominator");
  34.  
  35. // Simplify the fraction to the canonical form
  36. if (den.signum() == -1) {
  37. num = num.negate();
  38. den = den.negate();
  39. }
  40.  
  41. BigInteger gcd = num.gcd(den);
  42. if (!gcd.equals(BigInteger.ONE)) {
  43. num = num.divide(gcd);
  44. den = den.divide(gcd);
  45. }
  46.  
  47. this.numerator = num;
  48. this.denominator = den;
  49. }
  50.  
  51. public Fraction multiply(Fraction frac)
  52. {
  53. BigInteger num = this.numerator.multiply(frac.numerator);
  54. BigInteger den = this.denominator.multiply(frac.denominator);
  55.  
  56. BigInteger gcd = num.gcd(den);
  57. if (!gcd.equals(BigInteger.ONE)) {
  58. num = num.divide(gcd);
  59. den = den.divide(gcd);
  60. }
  61. return new Fraction(num,den);
  62. }
  63.  
  64. public Fraction divide(Fraction frac)
  65. {
  66. BigInteger num = this.numerator.multiply(frac.denominator);
  67. BigInteger den = this.denominator.multiply(frac.numerator);
  68.  
  69. BigInteger gcd = num.gcd(den);
  70. if (!gcd.equals(BigInteger.ONE)) {
  71. num = num.divide(gcd);
  72. den = den.divide(gcd);
  73. }
  74.  
  75. return new Fraction(num,den);
  76. }
  77.  
  78. public Fraction add(Fraction frac)
  79. {
  80. BigInteger num = this.numerator.multiply(frac.denominator).add(frac.numerator.multiply(denominator));
  81. BigInteger den = this.denominator.multiply(frac.denominator);
  82.  
  83. BigInteger gcd = num.gcd(den);
  84. if (!gcd.equals(BigInteger.ONE)) {
  85. num = num.divide(gcd);
  86. den = den.divide(gcd);
  87. }
  88.  
  89. return new Fraction(num, den);
  90. }
  91.  
  92. public Fraction subtract(Fraction frac)
  93. {
  94. BigInteger num = this.numerator.multiply(frac.denominator).subtract(frac.numerator.multiply(denominator));
  95. BigInteger den = this.denominator.multiply(frac.denominator);
  96.  
  97. BigInteger gcd = num.gcd(den);
  98.  
  99. if (!gcd.equals(BigInteger.ONE)) {
  100. num = num.divide(gcd);
  101. den = den.divide(gcd);
  102. }
  103.  
  104. return new Fraction(num, den);
  105. }
  106.  
  107. public static Fraction zero()
  108. {
  109. BigInteger num = BigInteger.ZERO;
  110. BigInteger den = BigInteger.ONE;
  111.  
  112. return new Fraction(num, den);
  113. }
  114.  
  115. public boolean greater(Fraction frac)
  116. {
  117. return this.compareTo(frac) == 1;
  118. }
  119.  
  120. public Fraction abs()
  121. {
  122. BigInteger num = this.numerator;
  123.  
  124. if(num.signum() == -1) {
  125. num = num.negate();
  126. }
  127.  
  128. return new Fraction(num, this.denominator);
  129. }
  130.  
  131. public boolean equals(Object obj)
  132. {
  133. if (!(obj instanceof Fraction)) {
  134. return false;
  135. }
  136.  
  137. Fraction other = (Fraction) obj;
  138. return this.numerator.equals(other.numerator) && this.denominator.equals(other.denominator);
  139. }
  140.  
  141. public int hashCode()
  142. {
  143. return this.numerator.hashCode() + this.denominator.hashCode();
  144. }
  145.  
  146. public int compareTo(Fraction other)
  147. {
  148. return this.numerator.multiply(other.denominator).compareTo(other.numerator.multiply(this.denominator));
  149. }
  150.  
  151. public String toString()
  152. {
  153. if (this.denominator.equals(BigInteger.ONE)) {
  154. return String.format("%d", this.numerator);
  155. } else {
  156. return String.format("%d/%d", this.numerator, this.denominator);
  157. }
  158. }
  159.  
  160. public Float toFloat()
  161. {
  162. return this.numerator.floatValue() / this.denominator.floatValue();
  163. }
  164.  
  165. public Double toDouble()
  166. {
  167. int SCALE = 16; // number of digits after the decimal place
  168.  
  169. BigDecimal num = new BigDecimal(this.numerator);
  170. BigDecimal den = new BigDecimal(this.denominator);
  171. BigDecimal quotient = num.divide(den, SCALE, RoundingMode.HALF_EVEN);
  172.  
  173. return quotient.doubleValue();
  174. }
  175.  
  176. /**
  177. * Gettery dla własności ułamka: licznik i mianownik
  178. */
  179. public BigInteger getNumerator()
  180. {
  181. return this.numerator;
  182. }
  183.  
  184. public BigInteger getDenominator()
  185. {
  186. return this.denominator;
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement