# Rational numbers - problem with comparsions

• 01-03-2010, 06:36 PM
omygoodness
Rational numbers - problem with comparsions
Hi,
I have a problem with one of the comparsions... "lessThanOrEqual" works fine for rational numbers like (3,1), (2,3) etc...
But if I try to compare for example (-2,1) and (1,-1) or (2,1) (1,-1) I get wrong answer... I have no idea how to rewrite this piece of code...
Code:

```import javax.swing.JOptionPane; public class Rational {       public static Rational ZERO = new Rational(0);     public static Rational ONE = new Rational(1);       private int numerator; // the numerator     private int denominator; // the denominator           public Rational(int numerator, int denominator) {                 // dealing with x/0 throwing exception           if (denominator == 0) {             throw new IllegalArgumentException("Denominator must be > 0");             }                   this.numerator = numerator;         this.denominator = denominator;         }               // input constructor         public Rational() {                   int num,den;         String numStr,denStr;         while(true)         {             try             {                 numStr=JOptionPane.showInputDialog(null,"Input numerator:");                 if (numStr==null)                 System.exit(0);                 num = Integer.parseInt(numStr);                 denStr=JOptionPane.showInputDialog(null,"Input denominator:");                 if (denStr==null)                 System.exit(0);                 den = Integer.parseInt(denStr);                 if(den == 0)                 {                     // dealing with x/0 throwing exception                     throw new                         IllegalArgumentException("Denominator must be > 0");                 }                 break;             }             catch(Exception e)             {                 System.out.println(e);             }                   }               numerator = num;         denominator = den;               }         // double constructor         public Rational (double d) {                               int offset=1;                 while(d*offset!=(int)(d*offset))                 {                     offset*=10;                 }                 numerator=(int)(d*offset);                 denominator=offset;                                              }                   // string constructor            public Rational(String numerator, String denominator) {                       int num = Integer.parseInt(numerator);             int den = Integer.parseInt(denominator);             if(den == 0)                               {                        // dealing with x/0 throwing exception                     throw new                     IllegalArgumentException("Denominator must be > 0");                 }                           this.numerator = num;             this.denominator = den;                   }                  // adding methods         public Rational add(Rational arg) {         return new Rational(arg.numerator*denominator+numerator*arg.denominator, denominator*arg.denominator);               }               public Rational add(int i) {         return new Rational(i*denominator+numerator,denominator);               }                  public Rational add(double d) {         Rational addedVal = new Rational(d);         return new Rational(addedVal.numerator*denominator+numerator*addedVal.denominator,denominator*addedVal.denominator);               }         // end of adding methods                              // multiplication methods         public Rational mul(Rational arg) {         return new Rational(arg.numerator*numerator, arg.denominator*denominator);                               }               public Rational mul(int i) {         return new Rational(i*numerator,denominator);               }               public Rational mul(double d) {         Rational mulVal=new Rational(d);         return new Rational(mulVal.numerator*numerator, mulVal.denominator*denominator);               }         // end of multiplications               // subtraction methods         public Rational sub(Rational arg) {         return new Rational(arg.numerator*denominator-numerator*arg.denominator, denominator*arg.denominator);               }               public Rational sub(int i) {         return new Rational(numerator-i*denominator,denominator);         }           public Rational sub(double d) {         Rational subVal=new Rational(d);         return new Rational(subVal.numerator*denominator-numerator*subVal.denominator, denominator*subVal.denominator);               }         // end of subtraction               // division methods         public Rational div(Rational arg) {         if(arg.numerator == 0)         {             throw new ArithmeticException("Dividing by 0 bad idea;)");         }               int newDen=denominator*arg.numerator;         int newNum=numerator*arg.denominator;         return new Rational(newNum, newDen);         }           public Rational div(int i) {         if(i == 0)         {             throw new ArithmeticException("Dividing by 0 bad idea;)");         }         return new Rational(numerator, denominator/i);               }               public Rational div(double d) {         if(d == 0)         {             throw new ArithmeticException("Dividing by 0 bad idea;)");         }               Rational divVal=new Rational(d);         int newDen=denominator*divVal.numerator;         int newNum=numerator*divVal.denominator;         return new Rational(newNum, newDen);               }         // end of division               // comparison methods         public boolean equals(Rational arg) {         return arg.numerator*denominator==arg.denominator*numerator;               }               public boolean lessThan(Rational arg) {         if (arg.denominator*numerator<denominator*arg.numerator) return true;               else                 return false;               }           public boolean greaterThan(Rational arg) {         if (arg.denominator*numerator>denominator*arg.numerator) return true;               else                 return false;               }         // problem with (-2,1), (1,-1) etc...         public boolean lessThanOrEqual(Rational arg) {         if (denominator*arg.numerator<=arg.denominator*numerator) return true;               else                 return false;               }           public boolean greaterThanOrEqual(Rational arg) {         if (arg.denominator*numerator>=denominator*arg.numerator) return true;               else                 return false;                       }         // end of comparsion             public String toString()       {           Rational red=reduce();           return red.numerator+"\\"+red.denominator;       }       // Working reduction       // I don't know how to implement "void reduce()"       private Rational reduce() //       {           int n = numerator;           int gcd = denominator;           int tmp;           if(n < gcd)           {             tmp = n;             n = gcd;             gcd = tmp;            }           while(n!=0)           {             tmp = n;             n = gcd%n;             gcd = tmp;           }           return new Rational(numerator/gcd,denominator/gcd);       }               public static void main(String[] args) {                       Rational a=new Rational();             Rational i=new Rational(3,1);             Rational d=new Rational(1,1);             Rational s=new Rational("25", "75");             System.out.println(i.lessThanOrEqual(d));                             /* Printing results            JOptionPane.showMessageDialog(null,"DEFINED RATIONAL NUMBERS:\n-------------------------------------------\n"         + "User rational number: "+a + "\nRational [1] (integer): "+i + "\nRational [2] (double): "+d + "\nRational [3] (string): "+s         + "\n-------------------------------------------\nADDITION:\n-------------------------------------------\n"               // Addition         +a + " + " +i+ " = " + a.add(i) + "\n"            +a + " + " +d+ " = " + a.add(d) + "\n"         +a + " + " +s+ " = " + a.add(s) + "\n"                  +d + " + " +s+ " = " + d.add(s) + "\n"                   + "-------------------------------------------\nSUBTRACTION:\n-------------------------------------------\n"                   // Subtraction         +i + " - " +a+ " = " + a.sub(i) + "\n"            +d + " - " +a+ " = " + a.sub(d) + "\n"         +s + " - " +a+ " = " + a.sub(s) + "\n"              +d + " - " +s+ " = " + s.sub(d) + "\n"                   + "-------------------------------------------\nMULTIPLICATION:\n-------------------------------------------\n"               // Multiplication            +a + " * " +i+ " = " + a.mul(i) + "\n"            +a + " * " +d+ " = " + a.mul(d) + "\n"         +a + " * " +s+ " = " + a.mul(s) + "\n"              +d + " * " +s+ " = " + s.mul(d) + "\n"                          + "-------------------------------------------\nDIVISION:\n-------------------------------------------\n"               // Division            +a + " : " +i+ " = " + a.div(i) + "\n"            +a + " : " +d+ " = " + a.div(d) + "\n"         +a + " : " +s+ " = " + a.div(s) + "\n"              +d + " : " +s+ " = " + s.div(d) + "\n"         + "-------------------------------------------\nCOMPARSIONS:\n-------------------------------------------\n"               // Comparsions            +a + " = " +i+ " = " + a.equals(i) + "\n"            +a + " < " +d+ " = " + a.lessThan(d) + "\n"         +a + " > " +s+ " = " + a.greaterThan(s) + "\n"              +a + " <= " +s+ " = " + a.lessThanOrEqual(s) + "\n"            +a + " >= " +d+ " = " + a.greaterThanOrEqual(d) + "\n", "Results",         JOptionPane.PLAIN_MESSAGE);         */                }        }```
• 01-03-2010, 06:44 PM
JosAH
Normalize your rationals n/d such that the denominator d is always > 0. A negative rational number is represented by a negative numerator. Let 'op' be any of the operators <, >, != or ==. If the relation n1/d1 op n2/d2 holds then the relation d2*n1 op d1*n2 also holds. e.g. 3/5 < 2/3 because 3*3 < 2*5

kind regards,

Jos
• 01-03-2010, 06:48 PM
omygoodness
Please modify my code if you can because I still don't know what I have to change :(
Thank you
• 01-03-2010, 06:56 PM
JosAH
Quote:

Originally Posted by omygoodness
Please modify my code if you can because I still don't know what I have to change :(
Thank you

Code:

``` // assume both are positive this.numerator = Math.abs(num); this.denominator = Math.abs(den); if ((num < 0) != (den < 0)) // exactly one of them is negative?   numerator= -numerator;```
Now any negative rational number is represented by a positive denominator and a negative numerator. That allows for the comparison trick I sketched to be easily implemented.

kind regards,

Jos
• 01-03-2010, 07:41 PM
omygoodness
I have many constructors and what is "Math" - I don't need import something?
I am beginner so please be patient :)

And what I have to do with:
Code:

```    public boolean lessThanOrEqual(Rational arg) {         if (denominator*arg.numerator<=arg.denominator*numerator) return true;               else                 return false;               }```
• 01-03-2010, 07:58 PM
JosAH
Quote:

Originally Posted by omygoodness
I have many constructors and what is "Math" - I don't need import something?
I am beginner so please be patient :)

And what I have to do with:
Code:

```    public boolean lessThanOrEqual(Rational arg) {         if (denominator*arg.numerator<=arg.denominator*numerator) return true;               else                 return false;               }```

You don't need to explicitly import the Math class; it is imported by default so you can use it anytime (it is a class with just static methods so you don't even have to create an object from that class).

Your method is almost correct; you'll find out when you try it; the changes are trivial. (reread my original reply on this matter, all you have to do is change the numerator and denominator for both objects as far as I can see).

kind regards,

Jos
• 01-03-2010, 08:11 PM
omygoodness
You can kill me but I am trying and I cant get proper result...
I know what I have to do but I don't know how implement your solution.

Another clue?
• 01-03-2010, 08:20 PM
JosAH
Quote:

Originally Posted by omygoodness
You can kill me but I am trying and I cant get proper result...
I know what I have to do but I don't know how implement your solution.

Another clue?

Just do the math: if n1/d1 <= n2/d2 then (multiply both sides by d2): d2*n1/d1 <= n2 and (now multiply both sides by d1): n1*d2 <= n2*d1.

If the 'other' rational 'arg' is n2/d2 then 'this' rational is n1/d1; compare it with your code and you'll see your mistake. Of course both d1 and d2 have to be > 0.

kind regards,

Jos
• 01-03-2010, 08:42 PM
omygoodness
You are trying to show me the way but I can't do this... I have no idea.
I understand what I should do but can't do this in Java so if you can send me a proper solution? Can you write simple program with this function? where I can see how it works.

Thank you
• 01-03-2010, 09:20 PM
JosAH
Quote:

Originally Posted by omygoodness
You are trying to show me the way but I can't do this... I have no idea.
I understand what I should do but can't do this in Java so if you can send me a proper solution? Can you write simple program with this function? where I can see how it works.

Thank you

Don't give up so soon; your method has to look like this:

Code:

```public boolean lessThanOrEqual(Rational arg) {   if (arg.denominator*numerator<=arg.numerator*denominator)       return true;   else       return false; }```
kind regards,

Jos