Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. //Name: Matthew Nguyen
  2. //Date: 11/18/19
  3. //Purpose: Do stuff with fractions like add, sub, multiply or divide them
  4.  
  5. public class Fraction
  6. {
  7. private int num; //Makes instance variable num
  8. private int denom;//Makes instance variable denom
  9. private Fraction answer; //Makes instance variable answer
  10.  
  11. public Fraction(int n, int d)
  12. {
  13. num = n; //Sets n equal to num
  14. denom = d;//Sets d equal to denom
  15.  
  16. }
  17.  
  18. public String toString() //toString to print out the objects
  19.  
  20. {
  21. String fraction;
  22. if(denom == 0)//Undefined checker
  23. {
  24. fraction = "undefined";
  25. }
  26. else if(num == 0)//Checks if numerator is 0
  27. {
  28. fraction = "0";
  29. }
  30. else if(denom == 1)//Checks if denominator is 1
  31. {
  32. fraction = ""+num;
  33. }
  34. else if(denom == -1 && num<0){
  35. fraction = ""+(-1*num);
  36. }
  37. else if(num < 0 && denom < 0)//check if both parts of fraction are negative
  38. {
  39. fraction = ""+(-1*num)+"/"+(-1*denom);
  40. }
  41. else
  42. {
  43. fraction = ""+num+"/"+denom;
  44. }
  45. return fraction;
  46. }
  47.  
  48.  
  49.  
  50. public int getNum()
  51. {
  52. return num;//returns int num when called
  53. }
  54.  
  55.  
  56.  
  57. public int getDen()
  58. {
  59. return denom;//returns int denom when called
  60. }
  61.  
  62.  
  63.  
  64. public boolean equals(Object other)
  65. {
  66.  
  67. Fraction temp = (Fraction) other; //Temp fraction to test if the things are equal
  68. int otherNum = temp.getNum(); //temp num and denominator to testif they are equal
  69. int otherDenom = temp.getDen();
  70. if((num*otherDenom) == (otherNum*denom))//cross multiply and if both sides are equal, then the fractions are equal
  71. {
  72. return true;
  73. }
  74. else//if they are not equal, a false is returned
  75. {
  76. return false;
  77. }
  78.  
  79. }
  80.  
  81.  
  82. private Fraction reduce()
  83.  
  84. {
  85. int answerNum;
  86. int answerDenom;
  87. int gcf = 1;
  88. for(int i = 1;i<=Math.min(Math.abs(num),Math.abs(denom));i++)
  89. {
  90. if((num%i==0) && (denom%i==0))
  91. {
  92. gcf = i;
  93. }
  94. }
  95. answerNum = getNum()/gcf;
  96. answerDenom= getDen()/gcf;
  97. Fraction reduced = new Fraction(answerNum, answerDenom);
  98. return reduced;
  99. }
  100.  
  101. public static Fraction reduced(Fraction reduced)
  102. {
  103. return reduced.reduce();
  104. }
  105.  
  106.  
  107.  
  108. public Fraction add(Fraction other)//method to add fractions
  109. {
  110. Fraction answer;
  111. int n = this.getNum()*other.getDen()+other.getNum()*this.getDen();//Function to produce numerator in instance of addition
  112. int d = this.getDen()*other.getDen(); //Function to produce denominator
  113. answer = new Fraction(n,d); //Creates a fraction with the new numerator and denominator when two fracs are added
  114.  
  115.  
  116. return reduced(answer); //Returns new fraction
  117.  
  118.  
  119. }
  120.  
  121. public Fraction sub(Fraction other)//method to subtract fractions
  122. {
  123. Fraction answer;
  124. int n = this.getNum()*other.getDen()-other.getNum()*this.getDen(); //Function to produce numerator in instance of subtraction
  125. int d = this.getDen()*other.getDen(); //Function to produce denominator
  126. answer = new Fraction(n,d); //Creates a fraction with the new numerator and denominator when two fracs are subtracted
  127.  
  128. return reduced(answer); //Returns new fraction
  129.  
  130. }
  131.  
  132. public Fraction mult(Fraction other)//method to multiply fractions
  133. {
  134. Fraction answer;
  135. int n = this.getNum()*other.getNum();
  136. int d = this.getDen()*other.getDen();
  137. answer = new Fraction (n,d);
  138.  
  139. return reduced(answer); //Returns new fraction
  140. }
  141.  
  142. public Fraction div(Fraction other)//method to divide fractions
  143. {
  144. int n = this.getNum()*other.getDen();
  145. int d = this.getDen()*other.getNum();
  146. Fraction answer;
  147. answer = new Fraction (n,d);
  148. Fraction undefined;
  149. undefined = new Fraction (0,0);
  150.  
  151. if(d==0)
  152. {
  153. return undefined;
  154. }
  155. else
  156. {
  157. return reduced(answer); //Returns new fraction
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement