ChaseKeskinyan

Fraction

Nov 1st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 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. }
  51.  
  52. public void setDenom(int d)
  53. {
  54. denom = d;
  55. }
  56.  
  57. public Fraction addFractions(Fraction other)
  58. {
  59. return new Fraction((this.num * other.denom + this.denom * other.num),(this.denom * other.denom));
  60. /*
  61. * Write reduce method
  62. * do Fraction f = new Fraction(bruhburh)
  63. * then do f.reduce()
  64. */
  65. }
  66.  
  67. public Fraction subtractFractions(Fraction other)
  68. {
  69. return this.addFractions(new Fraction(-other.num, other.denom));
  70. }
  71.  
  72. public Fraction multiplyFractions(Fraction other)
  73. {
  74. return new Fraction((this.num * other.num),(this.denom * other.denom));
  75. }
  76.  
  77. public Fraction divideFractions(Fraction other)
  78. {
  79. return this.multiplyFractions(new Fraction(other.denom,other.num));
  80. }
  81.  
  82. public String toString()
  83. {
  84. if(denom == 0)
  85. {
  86. return "undefined";
  87. }
  88.  
  89. else if(denom == 1)
  90. {
  91. return num + "";
  92. }
  93.  
  94. else if(num == 0)
  95. {
  96. return "0";
  97. }
  98.  
  99. else if(denom == -1)
  100. {
  101. return -num + "";
  102. }
  103.  
  104. else if(denom < 0)
  105. {
  106. return "(" + -num + "/" + -denom + ")";
  107. }
  108.  
  109. else
  110. {
  111. return "(" + num + "/" + denom + ")";
  112. }
  113.  
  114. }
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /**
  124. * Write a description of class FractionClient here.
  125. *
  126. * @author (your name)
  127. * @version (a version number or a date)
  128. */
  129. public class FractionClient
  130. {
  131. public static void main(String[]args)
  132. {
  133. Fraction attempt1 = new Fraction();
  134. Fraction attempt2 = new Fraction(3,4);
  135. Fraction attempt3 = new Fraction(5);
  136. Fraction attempt4 = new Fraction(attempt2);
  137. Fraction attempt5 = new Fraction(2,0);
  138. Fraction attempt6 = new Fraction(7,-1);
  139. Fraction attempt7 = new Fraction(6,-4);
  140. Fraction attempt8 = new Fraction(1,2);
  141.  
  142. System.out.println("My first attempt has the following points: " + attempt1);
  143. System.out.println("My second attempt has the following points: " + attempt2);
  144. System.out.println("My third attempt has the following points: " + attempt3);
  145. System.out.println("My fourth attempt has the following points: " + attempt4);
  146. System.out.println("My fifth attempt has the following points: " + attempt5);
  147. System.out.println("My sixth attempt has the following points: " + attempt6);
  148. System.out.println("My seventh attempt has the following points: " + attempt7);
  149. System.out.println("My eighth attempt has the following points: " + attempt8);
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment