Advertisement
Anon017706349

Rational

Jan 25th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Class description of the class Rational
  5. * The class Rational allows the user to do basic operations between two fractions
  6. *
  7. * @author Ryan Norng
  8. * @version version 1.0
  9. * @since 2020-01-26*/
  10.  
  11. public class Rational
  12. {
  13.  
  14. private int numerator;
  15. private int denominator;
  16.  
  17. /**
  18. * Default constructor. Creates a Rational object with a numerator and denominator of 1*/
  19. public Rational()
  20. {
  21. numerator = 1;
  22. denominator = 1;
  23. }
  24.  
  25. /**
  26. * Constructor. Creates a Rational object with specified numerator and denominator
  27. * @param num An integer specifying the numerator
  28. * @param den An integer specifying the denominator*/
  29. public Rational(int num, int den)
  30. {
  31. numerator = num;
  32. denominator = den;
  33. }
  34.  
  35. /** Gets the Rational object's numerator
  36. * @return Returns the integer representing the numerator*/
  37. public int getNumerator()
  38. {
  39. return numerator;
  40. }
  41.  
  42. /** Gets the Rational object's denominator
  43. * @return Returns the integer representing the denominator*/
  44. public int getDenominator()
  45. {
  46. return denominator;
  47. }
  48.  
  49. /** Sets the Rational object's numerator
  50. * @param value An integer representing the value to set the numerator to */
  51. public void setNumerator(int value)
  52. {
  53. numerator = value;
  54. }
  55.  
  56. /** Sets the Rational object's denominator
  57. * @param value An integer representing the value to set the denominator to*/
  58. public void setDenominator(int value)
  59. {
  60. denominator = value;
  61. }
  62.  
  63. /** Allows us to print the Rational object as a String
  64. * @return A String of the Rational object in the format of numerator/denominator*/
  65. public String toString()
  66. {
  67. String numstring = String.valueOf(numerator);
  68. String denstring = String.valueOf(denominator);
  69.  
  70. return numstring + "/" + denstring;
  71. }
  72.  
  73. /** Asks the user for inputs to set values of a Rational object. */
  74. public void inputRational()
  75. {
  76. Scanner ui = new Scanner(System.in);
  77. int userNum = 1;
  78. int userDen = 1;
  79. boolean numflag = false;
  80. boolean denflag = false;
  81. //input validation for non-zero integers (numerator)
  82. do // only allows int as an input
  83. {
  84. System.out.println("Please enter the numerator.");
  85. while (!ui.hasNextInt())
  86. {
  87. System.out.println("Invalid input. Please enter an integer.");
  88. ui.next();
  89. }
  90.  
  91. userNum = ui.nextInt();
  92. numflag = true;
  93.  
  94. }
  95. while (numflag == false || userNum == 0);
  96. //input validation for non-zero integers (denominator)
  97. do // only allows int as an input
  98. {
  99. System.out.println("Please enter the denominator.");
  100. while (!ui.hasNextInt())
  101. {
  102. System.out.println("Invalid input. Please enter an integer.");
  103. ui.next();
  104. }
  105.  
  106. userDen = ui.nextInt();
  107. denflag = true;
  108.  
  109. }
  110. while (denflag == false || userDen == 0);
  111.  
  112. // ui.close();
  113.  
  114. int gcd = 1;
  115. gcd = gcd(userNum,userDen);
  116.  
  117. numerator = userNum / gcd;
  118. denominator = userDen / gcd;
  119.  
  120. }
  121.  
  122. /** Finds gcd of two integers. Used to simply fractions of Rational objects
  123. * @return An integer representing the gcd of two integers*/
  124. private int gcd(int m, int n)
  125. {
  126. int r;
  127. while(n!=0)
  128. {
  129. r = m % n;
  130. m = n;
  131. n = r;
  132. }
  133.  
  134. return m;
  135. }
  136.  
  137. /** Adds two Rational objects
  138. * @param r1 A Rational object
  139. * @param r2 A Rational object*/
  140. public void add(Rational r1, Rational r2)
  141. {
  142. int gcd, tnum, tden;
  143. tnum = (r1.numerator * r2.denominator) + (r1.denominator * r2.numerator);
  144. tden = r1.denominator * r2.denominator;
  145. gcd = gcd(tnum,tden);
  146.  
  147. numerator = tnum / gcd;
  148. denominator = tden / gcd;
  149. }
  150. /** Subtracts a Rational object from another Rational object, and returns a Rational object
  151. * @param r A Rational object (the value being subtracted)
  152. * @return The Rational resulting from the subtraction*/
  153. public Rational sub(Rational r)
  154. {
  155. Rational tRational = new Rational();
  156. int gcd, tnum, tden;
  157. tnum = (numerator * r.denominator) - (denominator * r.numerator);
  158. tden = denominator * r.denominator;
  159. gcd = gcd(tnum,tden);
  160.  
  161. tRational.numerator = tnum / gcd;
  162. tRational.denominator = tden / gcd;
  163.  
  164. return tRational;
  165.  
  166. }
  167.  
  168. /** Multiplies two Rational objects
  169. * @param r1 A Rational object
  170. * @param r2 A Rational object*/
  171. public void mul(Rational r1, Rational r2)
  172. {
  173. int gcd, tnum, tden;
  174. tnum = r1.numerator * r2.numerator;
  175. tden = r1.denominator * r2.denominator;
  176. gcd = gcd(tnum,tden);
  177.  
  178. numerator = tnum / gcd;
  179. denominator = tden / gcd;
  180. }
  181.  
  182. /** Divides a Rational object from another Rational object, and returns a Rational object
  183. * @param r A Rational object (the value used for division)
  184. * @return The Rational resulting from the division*/
  185. public Rational div(Rational r)
  186. {
  187. Rational tRational = new Rational();
  188. int gcd, tnum, tden;
  189. tnum = numerator * r.denominator;
  190. tden = denominator * r.numerator;
  191. gcd = gcd(tnum,tden);
  192.  
  193. tRational.numerator = tnum / gcd;
  194. tRational.denominator = tden / gcd;
  195.  
  196. return tRational;
  197. }
  198.  
  199. /** Performs division between two Rational objects, and returns a decimal value as a double
  200. * @param r1 A Rational object
  201. * @param r2 a Rational object
  202. * @return A double representing the resulting value of the division between two Rational objects*/
  203. public static double divToDouble(Rational r1, Rational r2)
  204. {
  205. double tnum, tden;
  206. double realValue;
  207. tnum = r1.numerator * r2.denominator;
  208. tden = r1.denominator * r2.numerator;
  209. realValue = tnum / tden;
  210.  
  211. return realValue;
  212.  
  213. }
  214. }
  215.  
  216. public class CECS277Lab1 {
  217.  
  218. public static void main(String[] args)
  219. {
  220. Rational r1 = new Rational();
  221. Rational r2 = new Rational();
  222. Rational r3 = new Rational();
  223.  
  224. r1.inputRational();
  225. r2.inputRational();
  226.  
  227. r3.add(r1, r2);
  228.  
  229. System.out.println(r1 + " + " + r2 +" = " + r3);
  230.  
  231. r3 = r1.sub(r2);
  232.  
  233. System.out.println(r1 + " - " + r2 +" = " + r3);
  234.  
  235. r3.mul(r1, r2);;
  236.  
  237. System.out.println(r1 + " * " + r2 +" = " + r3);
  238.  
  239. r3 = r1.div(r2);
  240.  
  241. System.out.println(r1 + " / " + r2 +" = " + r3);
  242.  
  243. double d3 = Rational.divToDouble(r1, r2);
  244.  
  245. System.out.println(r1 + " + " + r2 +" = " + d3);
  246.  
  247. r1.setNumerator(2);
  248. System.out.println("The numerator of Rational r1 has been set to 2");
  249. r2.setDenominator(5);
  250. System.out.println("The denominator of Rational r2 has been set to 5");
  251.  
  252. System.out.println("Numerator: " + r1.getNumerator());
  253. System.out.println("Denominator: " + r2.getDenominator());
  254.  
  255. //System.out.println(r3);
  256. //System.out.print(d1);
  257.  
  258. }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement