Exemplo n.º 1
0
        /** Compare this ratio to object and return -1 if this is less than object,
         * 1 if this is greater than object, or zero if they are equal.
         *
         * @throws ClassCastException if object is not an IntRatio or Rational.
         */
        public int compareTo(Object obj)
        {
            if (obj is Rational)
            {
                // We know Rational.compareTo will call IntRatio on its
                //   own copy of an IntRatio, so use that.
                //   We are reversing the order of the compare, so reverse the result.
                return(-((Rational)obj).compareTo(this));
            }

            // This will throw the ClassCastException if the object is not IntRatio
            IntRatio r = (IntRatio)obj;

            // Make sure they are simplified and do the quick Equals check first.
            simplify();
            r.simplify();
            if (n == r.n && d == r.d)
            {
                return(0);
            }

            // Do this similar to sub, but we are only interested in the numerator.
            // (Since they are simplified, the denominators are positive.
            if ((n * r.d - r.n * d) < 0)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }