Advertisement
UniQuet0p1

Untitled

Oct 13th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /** This class represents fractions of form n/d where n and d are long integer
  4. * numbers. Basic operations and arithmetics for fractions are provided.
  5. */
  6. public class Lfraction implements Comparable<Lfraction> {
  7.  
  8. private static long numerator;
  9. private static long denominator;
  10.  
  11. /** Main method. Different tests. */
  12. public static void main (String[] param) {
  13. // TODO!!! Your debugging tests here
  14. }
  15.  
  16.  
  17. /** Constructor.
  18. * @param a numerator
  19. * @param b denominator > 0
  20. */
  21. public Lfraction (long a, long b) {
  22. long numerator = a;
  23. long denominator = Math.abs(b);
  24. }
  25.  
  26. /** Public method to access the numerator field.
  27. * @return numerator
  28. */
  29. public long getNumerator() {
  30. return Lfraction.numerator;
  31. }
  32.  
  33. /** Public method to access the denominator field.
  34. * @return denominator
  35. */
  36. public long getDenominator() {
  37. return Lfraction.denominator;
  38. }
  39.  
  40. /** Conversion to string.
  41. * @return string representation of the fraction
  42. */
  43. @Override
  44. public String toString() {
  45. return null; // TODO!!!
  46. }
  47.  
  48. /** Equality test.
  49. * @param m second fraction
  50. * @return true if fractions this and m are equal
  51. */
  52. @Override
  53. public boolean equals (Object m) {
  54. if ((minus((Lfraction)(m))).getNumerator() == 0)
  55. return true;
  56. else
  57. return false;
  58. }
  59.  
  60. /** Hashcode has to be equal for equal fractions.
  61. * @return hashcode
  62. */
  63. @Override
  64. public int hashCode() {
  65. return 0; // TODO!!!
  66. }
  67.  
  68. /** Sum of fractions.
  69. * @param m second addend
  70. * @return this+m
  71. */
  72. public Lfraction plus (Lfraction m) {
  73. long a = numerator * m.getDenominator() + denominator * m.getNumerator();
  74. long b = denominator * m.getDenominator();
  75. return new Lfraction(a, b);
  76. }
  77.  
  78. /** Multiplication of fractions.
  79. * @param m second factor
  80. * @return this*m
  81. */
  82. public Lfraction times (Lfraction m) {
  83. long a = numerator * m.getNumerator();
  84. long b = denominator * m.getDenominator();
  85. return new Lfraction(a, b);
  86. }
  87.  
  88. /** Inverse of the fraction. n/d becomes d/n.
  89. * @return inverse of this fraction: 1/this
  90. */
  91. public Lfraction inverse() {
  92. return null; // TODO!!!
  93. }
  94.  
  95. /** Opposite of the fraction. n/d becomes -n/d.
  96. * @return opposite of this fraction: -this
  97. */
  98. public Lfraction opposite() {
  99. return null; // TODO!!!
  100. }
  101.  
  102. /** Difference of fractions.
  103. * @param m subtrahend
  104. * @return this-m
  105. */
  106. public Lfraction minus (Lfraction m) {
  107. long a = numerator * m.getDenominator() - denominator * m.getNumerator();
  108. long b = denominator * m.getDenominator();
  109. return new Lfraction(a, b);
  110. }
  111.  
  112. /** Quotient of fractions.
  113. * @param m divisor
  114. * @return this/m
  115. */
  116. public Lfraction divideBy (Lfraction m) {
  117. return null; // TODO!!!
  118. }
  119.  
  120. /** Comparision of fractions.
  121. * @param m second fraction
  122. * @return -1 if this < m; 0 if this==m; 1 if this > m
  123. */
  124. @Override
  125. public int compareTo (Lfraction m) {
  126. if (this.minus(m).getNumerator() > 0)
  127. return 1;
  128. else if (this.minus(m).getNumerator() < 0)
  129. return -1;
  130. else
  131. return 0;
  132. }
  133.  
  134. /** Clone of the fraction.
  135. * @return new fraction equal to this
  136. */
  137. @Override
  138. public Object clone() throws CloneNotSupportedException {
  139. return null; // TODO!!!
  140. }
  141.  
  142. /** Integer part of the (improper) fraction.
  143. * @return integer part of this fraction
  144. */
  145. public long integerPart() {
  146. return 0L; // TODO!!!
  147. }
  148.  
  149. /** Extract fraction part of the (improper) fraction
  150. * (a proper fraction without the integer part).
  151. * @return fraction part of this fraction
  152. */
  153. public Lfraction fractionPart() {
  154. return null; // TODO!!!
  155. }
  156.  
  157. /** Approximate value of the fraction.
  158. * @return numeric value of this fraction
  159. */
  160. public double toDouble() {
  161. return numerator * 1.0 / denominator;
  162. }
  163.  
  164. /** Double value f presented as a fraction with denominator d > 0.
  165. * @param f real number
  166. * @param d positive denominator for the result
  167. * @return f as an approximate fraction of form n/d
  168. */
  169. public static Lfraction toLfraction (double f, long d) {
  170. return null; // TODO!!!
  171. }
  172.  
  173. /** Conversion from string to the fraction. Accepts strings of form
  174. * that is defined by the toString method.
  175. * @param s string form (as produced by toString) of the fraction
  176. * @return fraction represented by s
  177. */
  178. public static Lfraction valueOf (String s) {
  179. return null; // TODO!!!
  180. }
  181. }
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement