Results 1 to 10 of 10
 01032010, 06:36 PM #1Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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...
Java 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*denominatornumerator*arg.denominator, denominator*arg.denominator); } public Rational sub(int i) { return new Rational(numeratori*denominator,denominator); } public Rational sub(double d) { Rational subVal=new Rational(d); return new Rational(subVal.numerator*denominatornumerator*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); */ } }
 01032010, 06:44 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
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
 01032010, 06:48 PM #3Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
Please modify my code if you can because I still don't know what I have to change :(
Thank you
 01032010, 06:56 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
It's your assignment isn't it? Ok, I help you out halfway. Change the last part of your constructor to this:
Java 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;
kind regards,
Jos
 01032010, 07:41 PM #5Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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:
Java Code:public boolean lessThanOrEqual(Rational arg) { if (denominator*arg.numerator<=arg.denominator*numerator) return true; else return false; }
Last edited by omygoodness; 01032010 at 07:46 PM.
 01032010, 07:58 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
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
 01032010, 08:11 PM #7Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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?
 01032010, 08:20 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
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
 01032010, 08:42 PM #9Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
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
 01032010, 09:20 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Similar Threads

quadratic equation whith Rational class
By adamrain in forum New To JavaReplies: 8Last Post: 12222009, 06:35 PM 
Java program problem.. Arrays.. Random Numbers
By Chewart in forum New To JavaReplies: 16Last Post: 11162009, 11:21 PM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10152008, 12:46 AM 
Eclipse vs Rational Software Development
By tommy in forum Other IDEsReplies: 2Last Post: 05152008, 06:05 PM 
Problem comparing three numbers
By gammaman in forum New To JavaReplies: 1Last Post: 02092008, 06:43 PM
Bookmarks