Guest User

Untitled

a guest
Feb 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. * Rational Number Class
  3. * Prof. A.C. Silvestri
  4. * CSC-112 Intermediate Java
  5. * 02/08/18
  6. */
  7.  
  8. public class Rational implements Comparable<Rational> {
  9. private int num;
  10. private int den;
  11.  
  12. public final static Rational ZERO = new Rational(0);
  13. public final static Rational ONE = new Rational(1);
  14.  
  15. // ****************************************************************
  16.  
  17. public int getNumerator() {
  18. return num;
  19. }
  20.  
  21. public int getDenominator() {
  22. return den;
  23. }
  24.  
  25. // ****************************************************************
  26.  
  27. public Rational() {
  28. this(0, 1);
  29. }
  30.  
  31. public Rational(int num) {
  32. this(num, 1);
  33. }
  34.  
  35. public Rational(int numerator, int denominator) {
  36. if (denominator == 0) {
  37. throw new ArithmeticException("denominator is zero");
  38. }
  39.  
  40. // reduce fraction
  41. int g = gcf(numerator, denominator);
  42. num = numerator / g;
  43. den = denominator / g;
  44.  
  45. // needed only for negative numbers
  46. if (den < 0) {
  47. den = -den;
  48. num = -num;
  49. }
  50. }
  51.  
  52. // ****************************************************************
  53.  
  54. public Rational times(Rational b) {
  55. int num = this.num * b.num;
  56. int den = this.den * b.den;
  57. return new Rational(num, den);
  58. }
  59.  
  60. public Rational plus(Rational b) {
  61. Rational a = this;
  62.  
  63. // special cases (Really Not Needed)
  64. if (a.compareTo(Rational.ZERO) == 0)
  65. return b;
  66. if (b.compareTo(Rational.ZERO) == 0)
  67. return a;
  68.  
  69. int num = a.num * b.den + a.den * b.num;
  70. int den = a.den * b.den;
  71.  
  72. return new Rational(num, den);
  73. }
  74.  
  75. public Rational negate() {
  76. return new Rational(-this.num, this.den);
  77. }
  78.  
  79. public Rational abs() {
  80. if (num >= 0)
  81. return this;
  82. else
  83. return negate();
  84. }
  85.  
  86. public Rational minus(Rational b) {
  87. Rational a = this;
  88. return a.plus(b.negate());
  89. }
  90.  
  91. public Rational reciprocal() {
  92. return new Rational(den, num);
  93. }
  94.  
  95. public Rational divides(Rational b) {
  96. Rational a = this;
  97. return a.times(b.reciprocal());
  98. }
  99.  
  100. // ****************************************************************
  101.  
  102. public double toDouble() {
  103. return (double) num / den;
  104. }
  105.  
  106. public String toString() {
  107. if (den == 1)
  108. return num + "";
  109. else
  110. return num + "/" + den;
  111. }
  112.  
  113. // return { -, 0, + } if a < b, a = b, or a > b
  114. public int compareTo(Rational b) {
  115. Rational a = this;
  116. int lhs = a.num * b.den;
  117. int rhs = a.den * b.num;
  118. if (lhs < rhs)
  119. return -1;
  120. if (lhs > rhs)
  121. return +1;
  122. return 0;
  123. }
  124.  
  125. // is this Rational object equal to y?
  126. public boolean equals(Object y) {
  127. if (this == y)
  128. return true;
  129. if (y == null)
  130. return false;
  131. if (y.getClass() != this.getClass())
  132. return false;
  133. Rational b = (Rational) y;
  134. return compareTo(b) == 0;
  135. }
  136.  
  137. private static int gcf(int m, int n) {
  138. int retval = 0;
  139. if (m < 0)
  140. m = -m;
  141. if (n < 0)
  142. n = -n;
  143. if (n == 0)
  144. retval = m;
  145. else {
  146. int rem;
  147. while ((rem = m % n) != 0) {
  148. m = n;
  149. n = rem;
  150. }
  151. retval = n;
  152. }
  153. return retval;
  154. }
  155.  
  156. // Not needed or used, but just cool to see
  157. @SuppressWarnings("unused")
  158. private static int lcm(int m, int n) {
  159. if (m < 0)
  160. m = -m;
  161. if (n < 0)
  162. n = -n;
  163. return m * n / gcf(m, n);
  164. }
  165.  
  166. }
Add Comment
Please, Sign In to add comment