ChaseKeskinyan

Fraction

Nov 4th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1.  
  2. /**
  3. * Write a description of class Fraction here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class Fraction
  9. {
  10. private int num;
  11. private int denom;
  12.  
  13. public Fraction()
  14. {
  15. num = 0;
  16. denom = 1;
  17. }
  18.  
  19. public Fraction(int n, int d)
  20. {
  21. num = n;
  22. denom = d;
  23. }
  24.  
  25. public Fraction(Fraction other)
  26. {
  27. this.num = other.num;
  28. this.denom = other.denom;
  29. }
  30.  
  31. public Fraction(int n)
  32. {
  33. num = n;
  34. denom = 1;
  35. }
  36.  
  37. public int getNum()
  38. {
  39. return num;
  40. }
  41.  
  42. public int getDenom()
  43. {
  44. return denom;
  45. }
  46.  
  47. public void setNum(int n)
  48. {
  49. num = n;
  50. this.reduceFractions();
  51. }
  52.  
  53. public void setDenom(int d)
  54. {
  55. denom = d;
  56. this.reduceFractions();
  57. }
  58.  
  59. public void reduceFractions()
  60. {
  61. if( denom != 0)
  62. {
  63. int tmpN = Math.abs(num);
  64. int tmpD = Math.abs(denom);
  65. int tmp = tmpD;
  66. while(tmpD != 0)
  67. {
  68. tmp = tmpD;
  69. tmpD = tmpN % tmpD;
  70. tmpN = tmp;
  71. }
  72. num /=tmpN;
  73. denom /=tmpN;
  74. }
  75. }
  76.  
  77. public Fraction addFractions(Fraction other)
  78. {
  79. Fraction f = new Fraction((this.num * other.denom + this.denom * other.num),(this.denom * other.denom));
  80. f.reduceFractions();
  81. return f;
  82. }
  83.  
  84. public Fraction subtractFractions(Fraction other)
  85. {
  86. return this.addFractions(new Fraction(-other.num, other.denom));
  87. }
  88.  
  89. public Fraction multiplyFractions(Fraction other)
  90. {
  91. Fraction bruh = new Fraction((this.num * other.num),(this.denom * other.denom));
  92. bruh.reduceFractions();
  93. return bruh;
  94. }
  95.  
  96. public Fraction divideFractions(Fraction other)
  97. {
  98. return this.multiplyFractions(new Fraction(other.denom,other.num));
  99. }
  100.  
  101. public String toString()
  102. {
  103. if(denom == 0)
  104. {
  105. return "undefined";
  106. }
  107.  
  108. else if(denom == 1)
  109. {
  110. return num + "";
  111. }
  112.  
  113. else if(num == 0)
  114. {
  115. return "0";
  116. }
  117.  
  118. else if(denom == -1)
  119. {
  120. return -num + "";
  121. }
  122.  
  123. else if(denom < 0)
  124. {
  125. return "(" + -num + "/" + -denom + ")";
  126. }
  127.  
  128. else
  129. {
  130. return "(" + num + "/" + denom + ")";
  131. }
  132.  
  133. }
  134. }
  135.  
  136.  
  137.  
  138.  
  139. /**
  140. * Write a description of class FractionClient here.
  141. *
  142. * @author (your name)
  143. * @version (a version number or a date)
  144. */
  145. public class FractionClient
  146. {
  147. public static void main(String[]args)
  148. {
  149. Fraction attempt1 = new Fraction(1,2);
  150. Fraction attempt2 = new Fraction(3,4);
  151.  
  152. System.out.println("The sum of 1/2 and 3/4 is " + attempt1.addFractions(attempt2));
  153. System.out.println("The difference of 1/2 and 3/4 is " + attempt1.subtractFractions(attempt2));
  154. System.out.println("The product of 1/2 and 3/4 is " + attempt1.multiplyFractions(attempt2));
  155. System.out.println("The quotient of 1/2 and 3/4 is " + attempt1.divideFractions(attempt2));
  156.  
  157. Fraction fraction1 = new Fraction();
  158. System.out.println("Fraction1(Default) = " + fraction1);
  159.  
  160. Fraction fraction2 = new Fraction(3,4);
  161. System.out.println("Fraction2 = " + fraction2);
  162.  
  163. Fraction fraction3 = new Fraction(-1,2);
  164. System.out.println("Fraction3 = " + fraction3);
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment