/** * * @ author David Álvarez 789401 * @ email sly.dekar@gmail.com * @ date 01/09/05 * @ version 2.0 * **/ import java.io.*; class Rational { private int numerator, denominator; /* the numerator and denominator of the Rational Object */ /** *A constructor that sets the Rational to represent the number zero. The number zero is represented by a numerator field of 0 and a denominator field of 1. **/ public Rational() { this.numerator = 0; this.denominator = 1; } /** *A constructor that sets the rational to represent the whole number indicated by the parameter whole. For example, if the parameter whole were -4, it would be represented by a numerator field of -4 and a denominator field of 1. * *@Param whole the integer to be turned into rational. **/ public Rational(int numerator) { this.numerator = numerator; this.denominator = 1; } /** * * A constructor that sets the Rational to represent the fraction indicated by the parameters numer and denom. * The parameter numer indicates the numerator of the fraction to be represented and the parameter denom indicates the denominator * of the fraction to be represented. * * @param numer, denom integers which represent the numerator and denominator of the new Rational obj * **/ public Rational(int numer, int denom){ this.numerator = numer; this.denominator = denom; if (denom == 0) { this.numerator = 0; this.denominator = 1; } else { if (sgn(getDenominator())==-1) { setDenominator(getDenominator()*-1); setNumerator(getNumerator()*-1); } } reduce(); } /** * A constructor that sets the Rational to be an exact copy of the Rational that is the parameter r. * * @param r The Rational to be copied. **/ public Rational(Rational r) { this.numerator = r.numerator; this.denominator = r.denominator; reduce(); } /** *A method that returns the value of the field numerator. * *@return int the value of the field numerator. **/ public int getNumerator() { return this.numerator; } /** *A method that returns the value of the field denominator. * *@return int the value of the field denominator. **/ public int getDenominator() { return this.denominator; } /** *A method that sets the field numerator to the parameter numer. * *@param numer the new numerator (integer) **/ public void setNumerator(int numer) { this.numerator = numer; reduce(); } /** *A method that sets the field denominator to the parameter numer. * *@param denom the new denominator (Integer) **/ public void setDenominator(int denom) { this.denominator = denom; reduce(); } /** *A method that returns true if the invoking Rational represents a negative number; it returns false otherwise. * *@return boolean true if the invoking Rational represents a negative number; it returns false otherwise. **/ public boolean isNegative() { return (getNumerator()<0 || getDenominator()<0); } /** *A method that returns true if the invoking Rational represents zero, that is, if the numerator field is 0 and the denominator field is 1; it returns false otherwise. * *@return boolean true if the invoking Rational represents zero, that is, if the numerator field is 0 and the denominator field is 1; it returns false otherwise. **/ public boolean isZero() { return (getNumerator()==0); } /** *A method that returns true if the invoking Rational represents a positive number; it returns false otherwise. * *@return boolean true if the invoking Rational represents a positive number; it returns false otherwise. ***/ public boolean isPositive() { return (getNumerator()>0 && getDenominator()>0); } /** *A method that creates and returns a new Rational that represents the negation (additive opposite) of the invoking Rational * *@return n the opposite additive of the Rational. **/ public Rational makeNegative() { return new Rational(getNumerator()*-1, getDenominator()); } public Rational makePositive() { if (this.isNegative()) { return new Rational(getNumerator()*-1, getDenominator()); } return this; } /** * A method that creates and returns a new Rational that represents the reciprocal (multiplicative opposite) of the invoking Rational * *@return Rational the reciprocal. **/ public Rational reciprocal() { if (getNumerator() != 0) { return new Rational(getDenominator(), getNumerator()); } else { return this; } } /** *A method that creates and returns a new Rational that represents the sum of the invoking Rational and the Rational that is the parameter rhs. * *@param rhs a Rational to be added *@return Rational the sum of the two Rationals. **/ public Rational plus(Rational rhs) { Rational r = new Rational(this.getNumerator() * rhs.getDenominator() + this.getDenominator() * rhs.getNumerator(), this.getDenominator() * rhs.getDenominator()); r.reduce(); return r; } /** *A method that creates and returns a new Rational that is the difference of the invoking Rational and the Rational that is the parameter rhs. * *@param rhs a Rational object which will be substracted to the invoking Rational. *@return Rational a Rational object that represents the difference between both Rationals **/ public Rational minus(Rational rhs) { Rational r = new Rational(this.getNumerator() * rhs.getDenominator() - this.getDenominator() * rhs.getNumerator(), this.getDenominator() * rhs.getDenominator()); r.reduce(); return r; } /** *A method that creates and returns a new Rational that is the product of the invoking Rational and the Rational that is the parameter rhs. * *@param rhs a Rational object which will be multiplied to the invoking Rational. *@return Rational the multiplication of both Rational objects. **/ public Rational times(Rational rhs) { Rational r = new Rational(this.getNumerator()*rhs.getNumerator(), this.getDenominator()*rhs.getDenominator()); r.reduce(); return r; } /** *A method that creates and returns a new Rational that is the division of the invoking Rational by the Rational that is the parameter rhs. * *@param rhs a Rational object which will divide the invoking Rational object. *@return Rational the cocient of the Rational objects. **/ public Rational dividedBy(Rational rhs) { Rational r = new Rational(this.getNumerator()*rhs.getDenominator(), this.getDenominator()*rhs.getNumerator()); r.reduce(); return r; } /** *A method that returns true if the invoking Rational is equal to the Rational that is the parameter r; it returns false otherwise. Two Rational objects are equal if and only if their fields numerator and denominator are respectively equal. * *@param rhs the Rational object which will be compared with the invoking Rational object. *@return boolean TRUE if they're the same, FALSE if not **/ public boolean equals(Rational rhs) { rhs.reduce(); this.reduce(); return (getNumerator()==rhs.getNumerator() && getDenominator()==rhs.getDenominator()); } public boolean smallerThan(Rational rhs) { return (makeDouble(this) < makeDouble(rhs)); } public boolean greaterThan(Rational rhs) { return (makeDouble(this) > makeDouble(rhs)); } public double makeDouble(Rational r) { return r.getNumerator() / r.getDenominator(); } /** *A method that returns a string representation of the invoking Rational. * *@return String a string that represents the Rational. **/ public String toString() { if (this.isZero()) { return "0 "; } else { if (this.isNegative()) { if (this.getDenominator() == 1) { return getNumerator()+" "; } return (getNumerator() + "/" + getDenominator()); } else { if (this.getDenominator() == 1) { return getNumerator()+" "; } return (getNumerator() + "/" + getDenominator()); } } } /** * * This method reduces the invoking Rational to lowest terms. * **/ private void reduce() { if (numerator == 0) { denominator = 1; } else { int gcd = gcd(Math.abs(numerator), denominator); numerator /= gcd; denominator /= gcd; } } /** *This method calculates and returns the greatest common divisor *of the parameters a and b. *This use of this method must remain exclusive to the method reduce. * * @param a, b the two numbers whose greatest common divisor will be calculated. * @return a the greates common divisor (Integer) **/ private static int gcd(int a, int b) { while (a != b) { if (a > b) { a = a - b; } else { b = b - a; } } return a; } /** *This method returns the int value -1 if the parameter n is negative, *the int value 0 if the parameter n is zero, *the int value 1 if the parameter n is positive. * * @param n, integer. the number to be evaluated. * @return -1 if the parameter n is negative, * 0 if the parameter n is zero, * 1 if the parameter n is positive. **/ private static int sgn(int n) { return n == 0 ? 0 : n / Math.abs(n); } }