[java] abc

Viewer

  1. import java.lang.String;
  2. import java.lang.Math;
  3. import java.util.Scanner;
  4.  
  5. public class Solution {
  6.     private int numerator;
  7.     private int denominator = 1;
  8.  
  9.     /**.
  10.      * getNumerator
  11.      */
  12.     public int getNumerator() {
  13.         return numerator;
  14.     }
  15.  
  16.     /**.
  17.      * setNumerator
  18.      * @param numerator setNumerator
  19.      */
  20.     public void setNumerator(int numerator) {
  21.         this.numerator = numerator;
  22.     }
  23.  
  24.     /**.
  25.      * getDenominator
  26.      */
  27.     public int getDenominator() {
  28.         return denominator;
  29.     }
  30.  
  31.     /**.
  32.      * setDenominator
  33.      * @param denominator setDenominator
  34.      */
  35.     public void setDenominator(int denominator) {
  36.         if (denominator == 0) {
  37.             denominator = 1;
  38.         }
  39.         this.denominator = denominator;
  40.     }
  41.  
  42.     /**.
  43.      * Solution
  44.      */
  45.     public Solution() {
  46.         this.numerator = 1;
  47.     }
  48.  
  49.     /**.
  50.      * Solution
  51.      * @param numerator Solution
  52.      * @param denominator Solution
  53.      */
  54.     public Solution(int numerator, int denominator) {
  55.         this.numerator = numerator;
  56.         if (denominator == 0) {
  57.             denominator = 1;
  58.         }
  59.         this.denominator = denominator;
  60.     }
  61.  
  62.     /**.
  63.      * gcd
  64.      * @param a gcd
  65.      * @param b gcd
  66.      */
  67.     public static int gcd(int a, int b) {
  68.         if (== Integer.MAX_VALUE || b == Integer.MAX_VALUE
  69.                 || a == Integer.MIN_VALUE || b == Integer.MIN_VALUE) {
  70.             return 1;
  71.         }
  72.         if (< 0 || b < 0) {
  73.             a = Math.abs(a);
  74.             b = Math.abs(b);
  75.         }
  76.         if (== 0 || b == 0) {
  77.             return a + b;
  78.         }
  79.         while (!= b) {
  80.             if (> b) {
  81.                 a -= b;
  82.             } else {
  83.                 b -= a;
  84.             }
  85.         }
  86.         return a;
  87.     }
  88.  
  89.     /**.
  90.      * reduce
  91.      */
  92.     public Solution reduce() {
  93.         int ucln = gcd(this.getNumerator()this.getDenominator());
  94.         this.setNumerator(this.getNumerator() / ucln);
  95.         this.setDenominator(this.getDenominator() / ucln);
  96.  
  97.         return this;
  98.     }
  99.  
  100.     /**.
  101.      * add
  102.      * @param x add
  103.      */
  104.     public Solution add(Solution x) {
  105.         this.setNumerator(this.denominator * x.numerator + this.numerator * x.denominator);
  106.         this.setDenominator(this.denominator * x.denominator);
  107.  
  108.         return this.reduce();
  109.     }
  110.  
  111.     /**.
  112.      * subtract
  113.      * @param x subtract
  114.      */
  115.     public Solution subtract(Solution x) {
  116.         this.setNumerator(this.denominator * x.numerator - this.numerator * x.denominator);
  117.         this.setDenominator(this.denominator * x.denominator);
  118.  
  119.         return this.reduce();
  120.     }
  121.  
  122.     /**.
  123.      * multiply
  124.      * @param x multiply
  125.      */
  126.     public Solution multiply(Solution x) {
  127.         this.setNumerator(x.getNumerator() * this.getNumerator());
  128.         this.setDenominator(this.getDenominator() * x.getDenominator());
  129.  
  130.         return this.reduce();
  131.     }
  132.  
  133.     /**.
  134.      * divide
  135.      * @param x divide
  136.      */
  137.     public Solution divide(Solution x) {
  138.         this.setNumerator(this.getNumerator() * x.getDenominator());
  139.         this.setDenominator(this.getDenominator() * x.getNumerator());
  140.  
  141.         return this.reduce();
  142.     }
  143.  
  144.     /**.
  145.      * equals
  146.      * @param obj equals
  147.      */
  148.     @Override
  149.     public boolean equals(Object obj) {
  150.         if (obj instanceof Solution) {
  151.             Solution other = (Solution) obj;
  152.             if (other.reduce().getNumerator() == this.getNumerator()
  153.                     && other.reduce().getDenominator() == this.getDenominator()) {
  154.                 return true;
  155.             } else {
  156.                 return false;
  157.             }
  158.         } else {
  159.             return false;
  160.         }
  161.     }
  162. }
  163.  

Editor

You can edit this paste and save as new:


File Description
  • abc
  • Paste Code
  • 25 Sep-2021
  • 3.76 Kb
You can Share it: