Advertisement
Guest User

penis!

a guest
Nov 25th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. public class Rational{
  2.  
  3. private int n;
  4. private int d;
  5.  
  6. public Rational(){
  7. this.setNumerator(0);
  8. this.setDenominator(1);
  9. }
  10.  
  11. public Rational(int num){
  12. this.setNumerator(num);
  13. this.setDenominator(1);
  14. }
  15.  
  16. public Rational(int num, int denom){
  17. this.setNumerator(num);
  18. this.setDenominator(denom);
  19. this.reduce();
  20. }
  21.  
  22. public Rational( Rational r ){
  23. setNumerator(r.getNumerator());
  24. setDenominator(r.getDenominator());
  25. r.reduce();
  26. }
  27.  
  28. private int getNumerator(){
  29. return this.n;
  30. }
  31.  
  32. private int getDenominator(){
  33. return this.d;
  34. }
  35.  
  36. private void setNumerator(int num){
  37. n = num;
  38. }
  39.  
  40. private void setDenominator(int denom){
  41. if (denom != 0)
  42. d = denom;
  43. else
  44. throw new IllegalArgumentException("Denominator cannot be 0!");
  45. }
  46.  
  47. private int gcd(int x, int y){
  48. if (y==0)
  49. return x;
  50. else
  51. return gcd(y, x%y);
  52. }
  53.  
  54. private void reduce(){
  55. int divisor;
  56. divisor = gcd(Math.abs(n), Math.abs(d));
  57. System.out.println("DIVISOR IS: " + divisor);
  58. if (n<0 && d < 0){
  59. n*=-1;
  60. d*=-1;
  61. }
  62. else if (n > 0 && d < 0){
  63. n *= -1;
  64. d *= -1;
  65. }
  66. n = n/divisor;
  67. d = d/divisor;
  68. }
  69.  
  70. public Rational add( Rational r){
  71. int newNum;
  72. int newDenom;
  73. Rational rat;
  74. newNum = n*r.getDenominator()+d*r.getNumerator();
  75. newDenom = d*r.getDenominator();
  76. rat = new Rational(newNum, newDenom);
  77. rat.reduce();
  78. return rat;
  79. }
  80.  
  81. public Rational subtract(Rational r){
  82. int newNum;
  83. int newDenom;
  84. Rational rat;
  85. newNum = n*r.getDenominator()-d*r.getNumerator();
  86. newDenom = d*r.getDenominator();
  87. rat = new Rational(newNum, newDenom);
  88. rat.reduce();
  89. return rat;
  90. }
  91.  
  92. public Rational multiply(Rational r){
  93. int newNum;
  94. int newDenom;
  95. Rational rat;
  96. newNum = n*r.getNumerator();
  97. newDenom = d*r.getDenominator();
  98. rat = new Rational(newNum, newDenom);
  99. rat.reduce();
  100. return rat;
  101.  
  102. }
  103.  
  104. public Rational divide(Rational r){
  105. int newNum;
  106. int newDenom;
  107. Rational rat;
  108. newNum = n*r.getDenominator();
  109. newDenom = d*r.getNumerator();
  110. rat = new Rational(newNum, newDenom);
  111. rat.reduce();
  112. return rat;
  113.  
  114. }
  115.  
  116. public String toString(){
  117. if (n == 0)
  118. return "0";
  119. if (d == 1)
  120. return ""+this.getNumerator();
  121. return "" + n + "/" + d;
  122. }
  123.  
  124. public float toFloat(){
  125. float ratNum;
  126. ratNum = (float)n/d;
  127. return ratNum;
  128. }
  129.  
  130. public boolean equals(Object obj){
  131. if (obj.getClass() != this.getClass())
  132. return false;
  133. if (obj == null)
  134. return false;
  135. if (!(obj instanceof Rational))
  136. return false;
  137. else{
  138. Rational temp = (Rational) obj;
  139. if(n == temp.getNumerator()){
  140. if(d== temp.getDenominator())
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146.  
  147. public int hashCode(){
  148. return this.toString().hashCode();
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement