Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.math.RoundingMode;
- public final class Fraction implements Comparable<Fraction>
- {
- private BigInteger numerator;
- private BigInteger denominator;
- Fraction(int num)
- {
- this.initializeFraction(BigInteger.valueOf(num), BigInteger.ONE);
- }
- Fraction(BigInteger num, BigInteger den)
- {
- this.initializeFraction(num, den);
- }
- Fraction(String fraction)
- {
- String[] tokens = fraction.split("/");
- this.initializeFraction(
- new BigInteger(tokens[0]),
- new BigInteger(tokens[1])
- );
- }
- private void initializeFraction(BigInteger num, BigInteger den)
- {
- if (den.signum() == 0)
- throw new IllegalArgumentException("Zero denominator");
- // Simplify the fraction to the canonical form
- if (den.signum() == -1) {
- num = num.negate();
- den = den.negate();
- }
- BigInteger gcd = num.gcd(den);
- if (!gcd.equals(BigInteger.ONE)) {
- num = num.divide(gcd);
- den = den.divide(gcd);
- }
- this.numerator = num;
- this.denominator = den;
- }
- public Fraction multiply(Fraction frac)
- {
- BigInteger num = this.numerator.multiply(frac.numerator);
- BigInteger den = this.denominator.multiply(frac.denominator);
- BigInteger gcd = num.gcd(den);
- if (!gcd.equals(BigInteger.ONE)) {
- num = num.divide(gcd);
- den = den.divide(gcd);
- }
- return new Fraction(num,den);
- }
- public Fraction divide(Fraction frac)
- {
- BigInteger num = this.numerator.multiply(frac.denominator);
- BigInteger den = this.denominator.multiply(frac.numerator);
- BigInteger gcd = num.gcd(den);
- if (!gcd.equals(BigInteger.ONE)) {
- num = num.divide(gcd);
- den = den.divide(gcd);
- }
- return new Fraction(num,den);
- }
- public Fraction add(Fraction frac)
- {
- BigInteger num = this.numerator.multiply(frac.denominator).add(frac.numerator.multiply(denominator));
- BigInteger den = this.denominator.multiply(frac.denominator);
- BigInteger gcd = num.gcd(den);
- if (!gcd.equals(BigInteger.ONE)) {
- num = num.divide(gcd);
- den = den.divide(gcd);
- }
- return new Fraction(num, den);
- }
- public Fraction subtract(Fraction frac)
- {
- BigInteger num = this.numerator.multiply(frac.denominator).subtract(frac.numerator.multiply(denominator));
- BigInteger den = this.denominator.multiply(frac.denominator);
- BigInteger gcd = num.gcd(den);
- if (!gcd.equals(BigInteger.ONE)) {
- num = num.divide(gcd);
- den = den.divide(gcd);
- }
- return new Fraction(num, den);
- }
- public static Fraction zero()
- {
- BigInteger num = BigInteger.ZERO;
- BigInteger den = BigInteger.ONE;
- return new Fraction(num, den);
- }
- public boolean greater(Fraction frac)
- {
- return this.compareTo(frac) == 1;
- }
- public Fraction abs()
- {
- BigInteger num = this.numerator;
- if(num.signum() == -1) {
- num = num.negate();
- }
- return new Fraction(num, this.denominator);
- }
- public boolean equals(Object obj)
- {
- if (!(obj instanceof Fraction)) {
- return false;
- }
- Fraction other = (Fraction) obj;
- return this.numerator.equals(other.numerator) && this.denominator.equals(other.denominator);
- }
- public int hashCode()
- {
- return this.numerator.hashCode() + this.denominator.hashCode();
- }
- public int compareTo(Fraction other)
- {
- return this.numerator.multiply(other.denominator).compareTo(other.numerator.multiply(this.denominator));
- }
- public String toString()
- {
- if (this.denominator.equals(BigInteger.ONE)) {
- return String.format("%d", this.numerator);
- } else {
- return String.format("%d/%d", this.numerator, this.denominator);
- }
- }
- public Float toFloat()
- {
- return this.numerator.floatValue() / this.denominator.floatValue();
- }
- public Double toDouble()
- {
- int SCALE = 16; // number of digits after the decimal place
- BigDecimal num = new BigDecimal(this.numerator);
- BigDecimal den = new BigDecimal(this.denominator);
- BigDecimal quotient = num.divide(den, SCALE, RoundingMode.HALF_EVEN);
- return quotient.doubleValue();
- }
- /**
- * Gettery dla własności ułamka: licznik i mianownik
- */
- public BigInteger getNumerator()
- {
- return this.numerator;
- }
- public BigInteger getDenominator()
- {
- return this.denominator;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement