Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
5
Indexable
// Import your library
// Do not change the name of the Solution class
public class Solution {
    // Type your main code here


    private int numerator;
    private int denominator;

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public int getNumerator() {
        return numerator;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public int getDenominator() {
        return ((denominator == 0) ? 1 : denominator);
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public void setDenominator(int denominator) {
        this.denominator = ((denominator == 0) ? 1 : denominator);
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution add(Solution ps2) {
        this.numerator = this.numerator * ps2.denominator + this.denominator * ps2.numerator;
        this.denominator = this.denominator * ps2.denominator;
        return this;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution subtract(Solution ps2) {
        this.numerator = this.numerator * ps2.denominator - this.denominator * ps2.numerator;
        this.denominator = this.denominator * ps2.denominator;
        return this.reduce();
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution multiply(Solution ps2) {
        if (this.denominator != 0 && ps2.denominator != 0) {
            this.numerator = this.numerator * ps2.numerator;
            this.denominator = this.denominator * ps2.denominator;
            return this;
        }
        return this.reduce();
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution divide(Solution ps2) {
        if (ps2.getNumerator() == 0 || ps2.getDenominator() == 0) {
            return this;
        }
        this.numerator = this.numerator * ps2.denominator;
        this.denominator = this.denominator * ps2.numerator;
        return this.reduce();
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public Solution reduce() {
        int a = this.getNumerator();
        int b = this.getDenominator();
        int r = a % b;
        while (r != 0) {
            a = b;
            b = r;
            r = a % b;
        }
        this.numerator /= b;
        this.denominator /= b;
        return this;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public boolean equals(Object obj) {
        if (obj instanceof Solution) {
            Solution other = (Solution) obj;
            this.reduce();
            other.reduce();
            return (this.numerator == other.getNumerator()
                    && this.denominator == other.getDenominator());
        }
        return false;
    }

    /**
     *An abstract class that represents an algorithm.
     *
     * @author zhangtj
     *
     * @version 1.0
     */
    public static void main(String[] args) {

    }
}