fork(7) download
  1. import java.util.Scanner;
  2.  
  3. class Arithmetics {
  4. /**
  5. * Returns the greatest common divisor of two numbers
  6. * @param a the first number
  7. * @param b the second number
  8. * @return the greatest common divisor of two numbers
  9. */
  10. public static int gcd(int a, int b)
  11. {
  12. return (b == 0 ? a : gcd(b, a % b));
  13. }
  14. }
  15.  
  16. class Fraction {
  17. private int n, d;
  18.  
  19. /**
  20. * @param n is an integer number
  21. */
  22. public Fraction(int n) { setValue(n); }
  23. /**
  24. * @param n is the numerator of a fraction
  25. * @param d is the denominator of a fraction
  26. */
  27. public Fraction(int n, int d) throws IllegalArgumentException {
  28. setValue(n, d);
  29. }
  30. public Fraction(Fraction f) {
  31. setValue(f);
  32. }
  33.  
  34. public int getNumerator() { return n; }
  35. public int getDenominator() { return d; }
  36.  
  37. public void setNumerator(int n) { setValue(n, this.d); }
  38. public void setDenominator(int d) { setValue(this.n, d); }
  39. /**
  40. * Assigns a new fraction from an integer number
  41. */
  42. public void setValue(int n) { this.n = n; this.d = 1; }
  43. /**
  44. * Assigns a new value to the fraction
  45. * @param n is the new numerator of a fraction
  46. * @param d is the new denominator of a fraction
  47. */
  48. public void setValue(int n, int d) throws IllegalArgumentException {
  49. if (d == 0)
  50. throw new IllegalArgumentException("Error: Denominator cannot be equal to zero");
  51. this.n = n;
  52. this.d = d;
  53. reduce();
  54. }
  55. public void setValue(Fraction f) { this.n = f.n; this.d = f.d; }
  56.  
  57. private void reduce() {
  58. int gcd = Arithmetics.gcd(n, d);
  59. n /= gcd;
  60. d /= gcd;
  61. if (d < 0) { d *= -1; n *= -1; }
  62. }
  63. private void add(int n, int d) {
  64. this.n = this.n * d + n * this.d;
  65. this.d *= d;
  66. reduce();
  67. }
  68. private void substract(int n, int d) {
  69. add(n * -1, d);
  70. }
  71. private void multiply(int n, int d) {
  72. this.n *= n;
  73. this.d *= d;
  74. reduce();
  75. }
  76. private void divide(int n, int d) {
  77. this.n *= d;
  78. this.d *= n;
  79. reduce();
  80. }
  81.  
  82. /**
  83. * Adds an integer number to the fraction
  84. * @param n is a number
  85. */
  86. public void add(int n) {
  87. add(n, 1);
  88. }
  89. /** Adds another fraction to the fraction
  90. * @param f is another fracton
  91. */
  92. public void add(Fraction f) {
  93. add(f.n, f.d);
  94. }
  95.  
  96. /**
  97. * Subsracts an integer number from the fraction
  98. * @param n is a number
  99. */
  100. public void substract(int n) { add(n * -1); }
  101. /** Substracts another fraction from the fraction
  102. * @param f is another fracton
  103. */
  104. public void substract(Fraction f) {
  105. substract(f.n, f.d);
  106. }
  107.  
  108. /**
  109. * Multiplies the fraction by an integer number
  110. * @param n is a number
  111. */
  112. public void multiply(int n) {
  113. multiply(n, 1);
  114. }
  115. /** Multiplies the fraction by another fraction
  116. * @param f is another fracton
  117. */
  118. public void multiply(Fraction f) {
  119. multiply(f.n, f.d);
  120. }
  121.  
  122. /**
  123. * Divides the fraction by an integer number
  124. * @param n is a number
  125. */
  126. public void divide(int n) throws ArithmeticException {
  127. if (n == 0) throw new ArithmeticException("Error: Division by zero");
  128. divide(n, 1);
  129. }
  130. /** Divides the fraction by another fraction
  131. * @param f is another fracton
  132. */
  133. public void divide(Fraction f) throws ArithmeticException {
  134. if (f.n == 0) throw new ArithmeticException("Error: Division by zero");
  135. divide(f.n, f.d);
  136. }
  137.  
  138. /**
  139. * Compares the fraction to another fraction by equality
  140. * @param f is another fraction
  141. * @return true in case of equuality or false otherwise
  142. */
  143. public boolean equals(Fraction f) {
  144. return (n == f.n && d == f.d);
  145. }
  146. /**
  147. * Compares the fraction to another fraction
  148. * @param f is another fraction
  149. * @return 0 if the fraction equals to another fraction, 1 if greater, -1 if lesser
  150. */
  151. public int compareTo(Fraction f) {
  152. return (equals(f)) ? 0 : (n * f.d - f.n * d > 0) ? 1 : -1;
  153. }
  154.  
  155. /**
  156. * String representation of the fraction
  157. * @return the string representation of the fraction in such form: numerator/denominator
  158. */
  159. public String toString() {
  160. return n + "/" + d;
  161. }
  162. }
  163.  
  164. public class Main {
  165. public static void main(String[] args) {
  166. try {
  167. Scanner scanner = new Scanner(System.in);
  168. int n1 = scanner.nextInt();
  169. int d1 = scanner.nextInt();
  170. int n2 = scanner.nextInt();
  171. int d2 = scanner.nextInt();
  172.  
  173. Fraction f1 = new Fraction(n1, d1);
  174. String f1Str = f1.toString();
  175. System.out.println("The first fraction is " + f1Str);
  176. Fraction f2 = new Fraction(n2, d2);
  177. String f2Str = f2.toString();
  178. System.out.println("The second fraction is " + f2Str);
  179. Fraction temp = new Fraction(f1);
  180.  
  181. int comparisonResult = f1.compareTo(f2);
  182. char relationSign = (comparisonResult > 0) ? '>' : (comparisonResult < 0) ? '<' : '=';
  183. System.out.println(f1Str + " " + relationSign + " " + f2Str);
  184.  
  185. temp.add(f2);
  186. System.out.println(f1Str + " + " + f2Str + " = " + temp.toString());
  187. temp.setValue(f1);
  188.  
  189. temp.substract(f2);
  190. System.out.println(f1Str + " - " + f2Str + " = " + temp.toString());
  191. temp.setValue(f1);
  192.  
  193. temp.multiply(f2);
  194. System.out.println(f1Str + " * " + f2Str + " = " + temp.toString());
  195. temp.setValue(f1);
  196.  
  197. try {
  198. temp.divide(f2);
  199. System.out.println(f1Str + " / " + f2Str + " = " + temp.toString());
  200. } catch (ArithmeticException e) {
  201. System.out.println(e.getMessage());
  202. }
  203. System.out.println(e.getMessage());
  204. }
  205. }
  206. }
Success #stdin #stdout 0.07s 4386816KB
stdin
4
16
3
-5
stdout
The first fraction is 1/4
The second fraction is -3/5
1/4 > -3/5
1/4 + -3/5 = -7/20
1/4 - -3/5 = 17/20
1/4 * -3/5 = -3/20
1/4 / -3/5 = -5/12