# Need more help, this time requiring the Comparable interface

• November 13th, 2011, 09:55 PM
Mellowhype
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.

Code :

```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.
• November 14th, 2011, 06:00 AM
Norm
Re: Need more help, this time requiring the Comparable interface
Quote:

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.
• November 14th, 2011, 06:30 AM
Mr.777
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?
• November 14th, 2011, 06:49 AM
Norm
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?
• November 14th, 2011, 10:58 PM
Mr.777
Re: Need more help, this time requiring the Comparable interface
Quote:

Originally Posted by Norm
@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.