Advertisement
wicastle

Untitled

Dec 9th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package fractions;
  2. import java.util.*;
  3. public class Fraction
  4. {
  5. private Scanner scan = new Scanner(System.in);
  6. private int num=1;
  7. private int denom=1;
  8.  
  9. public Fraction()
  10. {
  11. }
  12.  
  13. public Fraction(int n, int d)
  14. {
  15. this.num = n;
  16. this.denom = d;
  17. }
  18.  
  19. public void setFraction(int n, int d)
  20. {
  21. this.num = n;
  22. this.denom = d;
  23. }
  24.  
  25. public Fraction add(Fraction op)
  26. {
  27. // Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d)
  28. op.num = (this.num * op.denom + this.denom * op.num);
  29. op.denom = (this.denom * op.denom);
  30. op.reduce();
  31. return op;
  32. }
  33.  
  34. public Fraction subtract(Fraction op)
  35. {
  36. // Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d)
  37. op.num = (this.num * op.denom - this.denom * op.num);
  38. op.denom = (this.denom * op.denom);
  39. op.reduce();
  40. return op;
  41. }
  42.  
  43. public Fraction multiply(Fraction op)
  44. {
  45. // Algebra HINT: a/b * c/d = (a*c)/ (b*d)
  46. op.num = (this.num * op.num);
  47. op.denom = (this.denom * op.denom);
  48. op.reduce();
  49. return op;
  50. }
  51.  
  52. public Fraction divide(Fraction op)
  53. {
  54. // Algebra HINT: a/b / c/d = (a*d)/ (b*c)
  55. this.num = (this.num * op.denom);
  56. this.denom = (this.denom * op.num);
  57. this.reduce();
  58. return this;
  59. }
  60.  
  61. private void reduce()
  62. {
  63. int smaller;
  64. if(this.num > this.denom)
  65. {
  66. smaller = denom;
  67. }
  68. else
  69. {
  70. smaller = num;
  71. }
  72. for(int divisor = 2; divisor < smaller; divisor++)
  73. {
  74. while ((num%divisor == 0)&&(denom%divisor == 0))
  75. {
  76. num /= divisor;
  77. denom /= divisor;
  78. }
  79. }
  80. }
  81.  
  82. public boolean equals(Fraction f)
  83. {
  84. f.reduce();
  85. this.reduce();
  86. if((this.num == f.num)&&(this.denom == f.denom))
  87. {
  88. return true;
  89. }
  90. else return false;
  91. }
  92.  
  93. public String toString()
  94. {
  95. this.reduce();
  96. return (this.num + "/" + this.denom);
  97. }
  98.  
  99. public void readin(String label)
  100. {
  101. while (true) // Keep trying if bad input is received
  102. {
  103. System.out.print(label);
  104. String temp = scan.next();
  105. temp = temp.trim(); // get rid of white space at the beginning and end
  106. int index = temp.indexOf('/');
  107. if (index >= 0)
  108. {
  109. String numStr = temp.substring(0, index);
  110. String denomStr = temp.substring(index+1);
  111. int n = Integer.parseInt(numStr);
  112. int d = Integer.parseInt(denomStr);
  113. setFraction(n,d);
  114. return;
  115. }
  116. else
  117. System.out.println("Input Fraction missing / ");
  118. }//Keep trying until you get it right
  119. }
  120.  
  121. public static void main(String[] args)
  122. {
  123. Fraction f1= new Fraction();
  124. Fraction f2= new Fraction();
  125. Fraction f3= null;
  126. Scanner scan = new Scanner(System.in);
  127.  
  128. while(true)
  129. {
  130. System.out.println(); // Add a blank line
  131. System.out.print("Enter operation: + - * / q (q ==> quit) : ");
  132. String input = scan.next();
  133. if (input.charAt(0) == 'q')
  134. break; // All done
  135.  
  136. f1.readin("Enter Fraction 1: ");
  137. f2.readin("Enter Fraction 2: ");
  138. System.out.println("f1 = " + f1);
  139. System.out.println("f2 = " + f2);
  140.  
  141. if (f1.equals(f2))
  142. System.out.println("f1 and f2 are equal");
  143. else
  144. System.out.println("f1 and f2 are not equal");
  145.  
  146. switch (input.charAt(0))
  147. {
  148. case '+':
  149. f3 = f1.add(f2);
  150. System.out.println("f1+f2=" + f3);
  151. break;
  152. case '-':
  153. f3 = f1.subtract(f2);
  154. System.out.println("f1-f2=" + f3);
  155. break;
  156. case '*':
  157. f3 = f1.multiply(f2);
  158. System.out.println("f1*f2="+f3);
  159. break;
  160. case '/':
  161. f3 = f1.divide(f2);
  162. System.out.println("f1/f2="+f3);
  163. break;
  164. default:
  165. System.out.println("Illegal command: " + input );
  166. break;
  167. }
  168. }// end of while loop
  169. System.out.println("Bye");
  170. scan.close();
  171. } // end of main
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement