Advertisement
UniQuet0p1

Lfraction

Oct 19th, 2021
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. //https://stackoverflow.com/questions/343584/how-do-i-get-whole-and-fractional-parts-from-double-in-jsp-java
  4. //https://www.javatpoint.com/java-integer-hashcode-method
  5. //https://www.fatalerrors.org/a/java-design-of-rational-number-class.html
  6. //https://introcs.cs.princeton.edu/java/92symbolic/Rational.java.html
  7. //https://stackoverflow.com/questions/474535/best-way-to-represent-a-fraction-in-java
  8. //https://codegym.cc/quests/lectures/questmultithreading.level01.lecture04
  9. //https://stackoverflow.com/questions/2563608/check-whether-a-string-is-parsable-into-long-without-try-catch
  10.  
  11. /** This class represents fractions of form n/d where n and d are long integer
  12.  * numbers. Basic operations and arithmetics for fractions are provided.
  13.  */
  14. public class Lfraction implements Comparable<Lfraction> {
  15.  
  16.    private final long numerator;
  17.    private final long denominator;
  18.  
  19.    /** Main method. Different tests. */
  20.    public static void main (String[] param) {
  21.       Lfraction f1 = new Lfraction (3, 5);
  22.       Lfraction f2 = new Lfraction (5, 7);
  23.       Lfraction prd;
  24.       prd = f1.times (f2);
  25.    }
  26.    
  27.  
  28.    /** Constructor.
  29.     * @param a numerator
  30.     * @param b denominator > 0
  31.     */
  32.    public Lfraction (long a, long b) {
  33.       numerator = a;
  34.       denominator = Math.abs(b);
  35.       if (b == 0){
  36.          throw new RuntimeException("Сannot be divided by 0");
  37.       }
  38.    }
  39.  
  40.    /** Public method to access the numerator field.
  41.     * @return numerator
  42.     */
  43.    public long getNumerator() {
  44.       return numerator;
  45.    }
  46.  
  47.    /** Public method to access the denominator field.
  48.     * @return denominator
  49.     */
  50.    public long getDenominator() {
  51.       return denominator;
  52.    }
  53.  
  54.    /** Conversion to string.
  55.     * @return string representation of the fraction
  56.     */
  57.    @Override
  58.    public String toString() {
  59.       if (getDenominator()==1) {
  60.          return getNumerator()+"";
  61.       }
  62.       else{
  63.          return getNumerator()+"/"+getDenominator();
  64.       }
  65.    }
  66.  
  67.    /** Equality test.
  68.     * @param m second fraction
  69.     * @return true if fractions this and m are equal
  70.     */
  71.    @Override
  72.    public boolean equals (Object m) {
  73.       return compareTo((Lfraction)m) == 0;
  74.    }
  75.  
  76.    /** Hashcode has to be equal for equal fractions.
  77.     * @return hashcode
  78.     */
  79.    @Override
  80.    public int hashCode() {
  81.       return Objects.hash(getNumerator(), getDenominator());
  82.    }
  83.  
  84.    /** Sum of fractions.
  85.     * @param m second addend
  86.     * @return this+m
  87.     */
  88.    public Lfraction plus (Lfraction m) {
  89.       long a = getNumerator() * m.getDenominator() +
  90.               getDenominator() * m.getNumerator();
  91.       long b = getDenominator() * m.getDenominator();
  92.       return new Lfraction(a, b);
  93.  
  94.       // getNumerator() -  первое левое верхнее число
  95.       // getDenominator() -  первое  левое нижнее число
  96.       // m.getNumerator() - второе правое верхнее число
  97.       // m.getDenominator() - второе правое нижнее число
  98.  
  99.    }
  100.  
  101.    /** Multiplication of fractions.
  102.     * @param m second factor
  103.     * @return this*m
  104.     */
  105.  
  106.  
  107.  
  108.    public Lfraction times (Lfraction m) {
  109.       long a = getNumerator() * m.getNumerator();
  110.       long b = getDenominator() * m.getDenominator();
  111.       System.out.println(getNumerator());
  112.       System.out.println(getDenominator());
  113.       System.out.println(m.getNumerator());
  114.       System.out.println(m.getDenominator());
  115.       System.out.println(a);
  116.       System.out.println(b);
  117.       System.out.println(b);
  118.       long gcd = gcd(a, b);
  119.       System.out.println(a / gcd);
  120.       System.out.println(b / gcd);
  121.       return new Lfraction((a / gcd),(b / gcd));
  122.    }
  123.  
  124.    private static long gcd(long a, long b) {
  125.       return b == 0 ? a : gcd(b, a % b);
  126.    }
  127.  
  128.  
  129.    /** Inverse of the fraction. n/d becomes d/n.
  130.     * @return inverse of this fraction: 1/this
  131.     */
  132.    public Lfraction inverse() {
  133.       if (getNumerator() == 0) {
  134.          throw new RuntimeException("The denominator must not be 0 after change of place");
  135.       }
  136.       if (getNumerator() < 0) {
  137.          return new Lfraction(-getDenominator(), -getNumerator());
  138.       }
  139.       return new Lfraction(getDenominator(), getNumerator());
  140.    }
  141.  
  142.    /** Opposite of the fraction. n/d becomes -n/d.
  143.     * @return opposite of this fraction: -this
  144.     */
  145.    public Lfraction opposite() {
  146.       if (getDenominator() == 0) {
  147.          throw new RuntimeException("The denominator must not be 0");
  148.       }
  149.       return new Lfraction(-getNumerator(), getDenominator());
  150.    }
  151.  
  152.    /** Difference of fractions.
  153.     * @param m subtrahend
  154.     * @return this-m
  155.     */
  156.    public Lfraction minus (Lfraction m) {
  157.       long a = getNumerator() * m.getDenominator() -
  158.               getDenominator() * m.getNumerator();
  159.       long b = getDenominator() * m.getDenominator();
  160.       return new Lfraction(a, b);
  161.    }
  162.  
  163.    /** Quotient of fractions.
  164.     * @param m divisor
  165.     * @return this/m
  166.     */
  167.    public Lfraction divideBy (Lfraction m) {
  168.          if (getDenominator() == 0) {
  169.             throw new RuntimeException("The denominator must not be 0");
  170.          }
  171.          if (m.getNumerator() == 0) {
  172.             throw new RuntimeException("The denominator must not be 0");
  173.          }
  174.          if (m.getNumerator() == 0 & m.getNumerator() == 0) {
  175.             throw new RuntimeException("The  multiplications of denominators must not be 0");
  176.          }
  177.          long a = getNumerator() *  m.getDenominator() ;
  178.          long b = getDenominator() * m.getNumerator();
  179.          long gcd = gcd(a, b);
  180.  
  181.          return new Lfraction((a / gcd),(b / gcd));
  182.  
  183.          //  8/3 % 6/9 = 8/3 * 9/6
  184.    }
  185.  
  186.    /** Comparision of fractions.
  187.     * @param m second fraction
  188.     * @return -1 if this < m; 0 if this==m; 1 if this > m
  189.     */
  190.    @Override
  191.    public int compareTo (Lfraction m) {
  192.       if (minus(m).getNumerator() < 0){
  193.          return -1;
  194.       }
  195.       else if (minus(m).getNumerator() == 0){
  196.          return 0;
  197.       }
  198.       else {
  199.          return 1;
  200.       }
  201.    }
  202.  
  203.  
  204.  
  205.    /** Clone of the fraction.
  206.     * @return new fraction equal to this
  207.     */
  208.    @Override
  209.    public Object clone()
  210.            throws CloneNotSupportedException {
  211.       return new Lfraction(getNumerator(), getDenominator());
  212.    }
  213.  
  214.    /** Integer part of the (improper) fraction.
  215.     * @return integer part of this fraction
  216.     */
  217.    public long integerPart() {
  218.       return (int)toDouble();
  219.    }
  220.  
  221.    /** Extract fraction part of the (improper) fraction
  222.     * (a proper fraction without the integer part).
  223.     * @return fraction part of this fraction
  224.     */
  225.    public Lfraction fractionPart() {
  226.       int part = (int) (getNumerator() / getDenominator());
  227.       return new Lfraction(getNumerator() - (part * getDenominator()), getDenominator());
  228.    }
  229.  
  230.    /** Approximate value of the fraction.
  231.     * @return numeric value of this fraction
  232.     */
  233.    public double toDouble() {
  234.       return getNumerator() * 1.0 / getDenominator();
  235.    }
  236.  
  237.    /** Double value f presented as a fraction with denominator d > 0.
  238.     * @param f real number
  239.     * @param d positive denominator for the result
  240.     * @return f as an approximate fraction of form n/d
  241.     */
  242.    public static Lfraction toLfraction (double f, long d) {
  243.       return new Lfraction(Math.round(f*10*d)/10, d);
  244.    }
  245.  
  246.    /** Conversion from string to the fraction. Accepts strings of form
  247.     * that is defined by the toString method.
  248.     * @param s string form (as produced by toString) of the fraction
  249.     * @return fraction represented by s
  250.     */
  251.    public static Lfraction valueOf (String s) {
  252.       if (s.isEmpty()) {
  253.          throw new RuntimeException("Input is empty");
  254.       }
  255.       String[] numbers = s.split("/");
  256.       if (numbers.length != 2){
  257.          throw new RuntimeException("The entered word must contain exactly two numbers, the input contains: \"" + s + "\"");
  258.       }
  259.       if (isDigit(numbers[0])){ // isDigit проверяет является ли символ числом
  260.          throw new RuntimeException("It is not a number: \"" + s + "\"" );
  261.       }
  262.       if(isDigit(numbers[1])){
  263.          throw new RuntimeException("Denominator is not number: \"" + s + "\"" );
  264.       }
  265.       return new Lfraction(Long.parseLong(numbers[0]), Long.parseLong(numbers[1]));
  266.    }
  267.  
  268.    private static boolean isDigit(String str) {  //votsin siit: https://stackoverflow.com/questions/2563608/check-whether-a-string-is-parsable-into-long-without-try-catch
  269.       if (str == null) {
  270.          return true;
  271.       }
  272.       int sz = str.length();
  273.       for (int i = 0; i < sz; i++) {
  274.          if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '-') {
  275.             return true;
  276.          }
  277.       }
  278.       return false;
  279.    }
  280. }
  281.  
  282.  
  283.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement