Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need more help, this time requiring the Comparable interface

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need more help, this time requiring the Comparable interface

    What I need to do is compare a floating point value from the numerator and denominator for both
    rational objects.

    public class Rational implements Comparable
    {
     
        public int compareTo(Object obj);
        {
     
        }
     
        private int numerator, denominator;
     
        //-----------------------------------------------------------------
        //  Sets up the rational number by ensuring a nonzero denominator
        //  and making only the numerator signed.
        //-----------------------------------------------------------------
        public Rational (int numer, int denom)
        {
            if (denom == 0)
                denom = 1;
     
            // Make the numerator "store" the sign
            if (denom < 0)
            {
                numer = numer * -1;
                denom = denom * -1;
            }
     
            numerator = numer;
            denominator = denom;
            reduce();
        }
     
        //-----------------------------------------------------------------
        //  Returns the numerator of this rational number.
        //-----------------------------------------------------------------
        public int getNumerator ()
        {
            return numerator;
        }
     
        //-----------------------------------------------------------------
        //  Returns the denominator of this rational number.
        //-----------------------------------------------------------------
        public int getDenominator ()
        {
            return denominator;
        }
     
        //-----------------------------------------------------------------
        //  Returns the reciprocal of this rational number.
        //-----------------------------------------------------------------
        public Rational reciprocal ()
        {
            return new Rational (denominator, numerator);
        }
     
        //-----------------------------------------------------------------
        //  Adds this rational number to the one passed as a parameter.
        //  A common denominator is found by multiplying the individual
        //  denominators.
        //-----------------------------------------------------------------
        public Rational add (Rational op2)
        {
            int commonDenominator = denominator * op2.getDenominator();
            int numerator1 = numerator * op2.getDenominator();
            int numerator2 = op2.getNumerator() * denominator;
            int sum = numerator1 + numerator2;
     
            return new Rational (sum, commonDenominator);
        }
     
        //-----------------------------------------------------------------
        //  Subtracts the rational number passed as a parameter from this
        //  rational number.
        //-----------------------------------------------------------------
        public Rational subtract (Rational op2)
        {
            int commonDenominator = denominator * op2.getDenominator();
            int numerator1 = numerator * op2.getDenominator();
            int numerator2 = op2.getNumerator() * denominator;
            int difference = numerator1 - numerator2;
     
            return new Rational (difference, commonDenominator);
        }
     
        //-----------------------------------------------------------------
        //  Multiplies this rational number by the one passed as a
        //  parameter.
        //-----------------------------------------------------------------
        public Rational multiply (Rational op2)
        {
            int numer = numerator * op2.getNumerator();
            int denom = denominator * op2.getDenominator();
            return new Rational (numer, denom);
        }
     
        //-----------------------------------------------------------------
        //  Divides this rational number by the one passed as a parameter
        //  by multiplying by the reciprocal of the second rational.
        //-----------------------------------------------------------------
        public Rational divide (Rational op2)
        {
            return multiply (op2.reciprocal());
        }
     
        //-----------------------------------------------------------------
        //  Determines if this rational number is equal to the one passed
        //  as a parameter. Assumes they are both reduced.
        //-----------------------------------------------------------------
        public boolean equals (Rational op2)
        {
            return ( numerator == op2.getNumerator() &&
                denominator == op2.getDenominator() );
        }
     
        //-----------------------------------------------------------------
        //  Returns this rational number as a string.
        //-----------------------------------------------------------------
        public String toString ()
        {
            String result;
     
            if (numerator == 0)
                result = "0";
            else
            if (denominator == 1)
                result = numerator + "";
            else
                result = numerator + "/" + denominator;
            return result;
        }
     
        //-----------------------------------------------------------------
        //  Reduces this rational number by dividing both the numerator
        //  and the denominator by their greatest common divisor.
        //-----------------------------------------------------------------
        private void reduce ()
        {
            if (numerator != 0)
            {
                int common = gcd (Math.abs(numerator), denominator);
     
                numerator = numerator / common;
                denominator = denominator / common;
            }
        }
     
        //-----------------------------------------------------------------
        //  Computes and returns the greatest common divisor of the two
        //  positive parameters. Uses Euclid's algorithm.
        //-----------------------------------------------------------------
        private int gcd (int num1, int num2)
        {
            while (num1 != num2)
                if (num1 > num2)
                    num1 = num1 - num2;
                else
                    num2 = num2 - num1;
     
            return num1;
        }
    }

    The problem is, I'm clueless as to what to do :\ I know how to actually make the comparison, it's just i don't know what to compare.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need more help, this time requiring the Comparable interface

    What I need to do is compare a floating point value
    Comparing floating point numbers is tricky because of their nature.
    To compare two numbers for "equality" you need to see if their values are very close to each other.
    For example: 0.0123456789 vs 0.0123456788 could be considered close enough to be equal.
    One way would be to take the difference of their absolute values and see if it is less than a threshhold value.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need more help, this time requiring the Comparable interface

    Well, you can also use ceil() or floor() function of Math class, in order to compare float values to the closest.

    Suggestion: Why don't you compare the rational values instead of float values?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need more help, this time requiring the Comparable interface

    @777 Can you give an example of how to use those Math methods to compare two floating point numbers?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need more help, this time requiring the Comparable interface

    Quote Originally Posted by Norm View Post
    @777 Can you give an example of how to use those Math methods to compare two floating point numbers?
    Did i mention it that you can use Math methods to compare? I said by using ceil() and floor(), you can be able to process the float values.

Similar Threads

  1. When to use Comparator and Comparable Interface in java
    By diyaots in forum Java Theory & Questions
    Replies: 7
    Last Post: October 26th, 2012, 09:35 AM
  2. Using Comparable in Binary Search
    By Shaybay92 in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 12th, 2011, 03:23 AM
  3. Help with Comparable interface.
    By novice564 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 19th, 2011, 08:39 AM
  4. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 10:02 PM
  5. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM