# Rational Numbers in java

• 07-12-2013, 04:58 PM
therealvasile
Rational Numbers in java
Heres the exercise im working on:

Write a class called Rational which has instance variables num and den which hold the numerator and denominator values such that the number is always expressed in reduced form. Include the following methods in the class Rational:

(a) A constructor that accepts two integer parameters a and b and creates a Rational object representing the rational number a=b.
(b) A constructor that accepts one integer parameter a and creates a Rational object representing the number a.
(c) A method called setRational which accepts two integer parameters a and b and sets the number to a/b.
(d) A method called setRational which accepts one integer parameter a and sets the number to a.
(e) A method called getNum that returns the numerator in the reduced form expression of the rational number.
(f) A method called getDen that returns the denominator in the reduced form expression of the rational number.
(g) A method called add which accepts two integers as parameters (say, c and d) and updates num (say, holding value a) and den (say, holding value b) such that num/den= a/b + c/d.
final num and den should be in reduced form.

Heres what ive done. Am i on the right track?

Code:

``` public class Rational {     int num;     int den;         public Rational(int a, int b)     {         if (b == 0){ b = 1; }         this.num = a;         this.den = b ;         Rational R1 = new Rational(a/b);     }         public Rational(int a)     {         this.num = a;         this.den = 1;         Rational R1 = new Rational(a);     }         public Rational setRational (int a, int b)     {         this.num = a;         this.den = b;         Rational R1 = new Rational(num/den);         return R1;     }         public Rational setRational (int a)     {         this.num = a;         this.den = 1;         Rational R1 = new Rational(num/den);         return R1;     }       public getNum         private static int gcd( int m, int n )     {         int mx = Math.max( m, n );         int mn = Math.min( m, n );                 int remainder = 1;         while ( remainder != 0 )         {             remainder = mx % mn;             mx = mn;             mn = remainder;         }         return mx;     }         public static void main(String[] args)     {             } }```
• 07-12-2013, 07:55 PM
kjkrum
Re: Rational Numbers in java
Not bad so far. I notice you didn't include a validity check in setRational(int, int). And I wouldn't change the argument if it's not valid; that violates the Principle of Least Surprise. Throw an IllegalArgumentException instead.

Edit: Just noticed what you're returning from setRational(int, int). Why are you creating a new object there? Setters are typically either 'void' methods, or they return 'this' if you want to do method chaining. And creating a new object in your constructor will create infinite recursion, resulting in a stack overflow or out of memory error. Don't do that; just assign the fields of 'this'.
• 07-12-2013, 08:33 PM
jim829
Re: Rational Numbers in java
As was already stated, don't create an object in your constructors or your methods. I recommend in your constructors,you do the fiollowing:

Code:

```public Rational(int num, int den) {     // some validity check here     int g = gcd(num, den);     this.num = num/g;     this.den = den/g; } public Rational (int num) {     this(num,1); }```
I also recommend you write a separate validity check that can be used by the set method and constructor. And you may want to store the numerator and denominator in reduced form.

Regards,
Jim