Advertisement
Guest User

Untitled

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