Advertisement
UniQuet0p1

Untitled

Oct 14th, 2021
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 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.    public static long gcd(long a, long b) {
  8.       return b == 0 ? a : gcd(b, a % b);
  9.    }
  10.  
  11.  
  12.    public long numerator;
  13.    public long denominator;
  14.  
  15.    /** Main method. Different tests. */
  16.    public static void main (String[] param) {
  17.     // TODO!!! Your debugging tests here
  18.    }
  19.    
  20.  
  21.    /** Constructor.
  22.     * @param a numerator
  23.     * @param b denominator > 0
  24.     */
  25.    public Lfraction (long a, long b) {
  26.       numerator = a;
  27.       denominator = Math.abs(b);
  28.       if (b == 0)
  29.          throw new RuntimeException("Сannot be divided by 0");
  30.    }
  31.  
  32.    /** Public method to access the numerator field.
  33.     * @return numerator
  34.     */
  35.    public long getNumerator() {
  36.       return numerator;
  37.    }
  38.  
  39.    /** Public method to access the denominator field.
  40.     * @return denominator
  41.     */
  42.    public long getDenominator() {
  43.       return denominator;
  44.    }
  45.  
  46.    /** Conversion to string.
  47.     * @return string representation of the fraction
  48.     */
  49.    @Override
  50.    public String toString() {
  51.       if (getDenominator()==1)
  52.          return getNumerator()+"";
  53.       else
  54.          return getNumerator()+"/"+getDenominator();
  55.    }
  56.  
  57.    /** Equality test.
  58.     * @param m second fraction
  59.     * @return true if fractions this and m are equal
  60.     */
  61.    @Override
  62.    public boolean equals (Object m) {
  63.       return compareTo((Lfraction)m) == 0;
  64.    }
  65.  
  66.    /** Hashcode has to be equal for equal fractions.
  67.     * @return hashcode
  68.     */
  69.    @Override
  70.    public int hashCode() {
  71.       return Objects.hash(getNumerator(), getDenominator());
  72.    }
  73.  
  74.    /** Sum of fractions.
  75.     * @param m second addend
  76.     * @return this+m
  77.     */
  78.    public Lfraction plus (Lfraction m) {
  79.       long a = getNumerator() * m.getDenominator() +
  80.               getDenominator() * m.getNumerator();
  81.       long b = getDenominator() * m.getDenominator();
  82.       return new Lfraction(a, b);
  83.    }
  84.  
  85.    /** Multiplication of fractions.
  86.     * @param m second factor
  87.     * @return this*m
  88.     */
  89.  
  90.  
  91.  
  92.    public Lfraction times (Lfraction m) {
  93.       long a = getNumerator() * m.getNumerator();
  94.       long b = getDenominator() * m.getDenominator();
  95.       long gcd = gcd(a, b);
  96.       return new Lfraction((a / gcd),(b / gcd));
  97.    }
  98.  
  99.  
  100.    /** Inverse of the fraction. n/d becomes d/n.
  101.     * @return inverse of this fraction: 1/this
  102.     */
  103.    public Lfraction inverse() {
  104.       if (numerator < 0) {
  105.          return new Lfraction(-denominator, -numerator);
  106.       }
  107.       return new Lfraction(denominator, numerator);
  108.    }
  109.  
  110.    /** Opposite of the fraction. n/d becomes -n/d.
  111.     * @return opposite of this fraction: -this
  112.     */
  113.    public Lfraction opposite() {
  114.       return new Lfraction(-numerator, denominator);
  115.    }
  116.  
  117.    /** Difference of fractions.
  118.     * @param m subtrahend
  119.     * @return this-m
  120.     */
  121.    public Lfraction minus (Lfraction m) {
  122.       long a = getNumerator() * m.getDenominator() - getDenominator() * m.getNumerator();
  123.       long b = getDenominator() * m.getDenominator();
  124.       return new Lfraction(a, b);
  125.    }
  126.  
  127.    /** Quotient of fractions.
  128.     * @param m divisor
  129.     * @return this/m
  130.     */
  131.    public Lfraction divideBy (Lfraction m) {
  132.          long a = getNumerator() *  m.getDenominator() ;
  133.          long b = getDenominator() * m.getNumerator();
  134.          long gcd = gcd(a, b);
  135.          return new Lfraction((a / gcd),(b / gcd));
  136.    }
  137.  
  138.    /** Comparision of fractions.
  139.     * @param m second fraction
  140.     * @return -1 if this < m; 0 if this==m; 1 if this > m
  141.     */
  142.    @Override
  143.    public int compareTo (Lfraction m) {
  144.       if (minus(m).getNumerator() > 0)
  145.          return 1;
  146.       else if (minus(m).getNumerator() < 0)
  147.          return -1;
  148.       else
  149.          return 0;
  150.    }
  151.  
  152.  
  153.  
  154.    /** Clone of the fraction.
  155.     * @return new fraction equal to this
  156.     */
  157.    @Override
  158.    public Object clone()
  159.            throws CloneNotSupportedException {
  160.       return new Lfraction(getNumerator(), getDenominator());
  161.    }
  162.  
  163.    /** Integer part of the (improper) fraction.
  164.     * @return integer part of this fraction
  165.     */
  166.    public long integerPart() {
  167.       return (int)toDouble();
  168.    }
  169.  
  170.    /** Extract fraction part of the (improper) fraction
  171.     * (a proper fraction without the integer part).
  172.     * @return fraction part of this fraction
  173.     */
  174.    public Lfraction fractionPart() {
  175.       int part = (int) (numerator / denominator);
  176.       return new Lfraction(numerator - (part * denominator), denominator);
  177.    }
  178.  
  179.    /** Approximate value of the fraction.
  180.     * @return numeric value of this fraction
  181.     */
  182.    public double toDouble() {
  183.       return numerator * 1.0 / denominator;
  184.    }
  185.  
  186.    /** Double value f presented as a fraction with denominator d > 0.
  187.     * @param f real number
  188.     * @param d positive denominator for the result
  189.     * @return f as an approximate fraction of form n/d
  190.     */
  191.    public static Lfraction toLfraction (double f, long d) {
  192.       long nominator = Math.round(Math.round(f*10*d)/10);
  193.       return new Lfraction(nominator, d);
  194.    }
  195.  
  196.    /** Conversion from string to the fraction. Accepts strings of form
  197.     * that is defined by the toString method.
  198.     * @param s string form (as produced by toString) of the fraction
  199.     * @return fraction represented by s
  200.     */
  201.    public static Lfraction valueOf (String s) {
  202.       if (s.isEmpty())
  203.          throw new RuntimeException("Input is empty");
  204.  
  205.       String[] numbers = s.split("/");
  206.  
  207.       if (numbers.length != 2)
  208.          throw new RuntimeException("The entered word must contain exactly two numbers, the input contains: \"" + s + "\"");
  209.       return new Lfraction(Long.parseLong(numbers[0]), Long.parseLong(numbers[1]));
  210.    }
  211. }
  212.  
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement