Results 1 to 3 of 3
Thread: Rational Numbers in java
 07122013, 04:58 PM #1Member
 Join Date
 Jul 2013
 Posts
 9
 Rep Power
 0
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?
Java 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) { } }
 07122013, 07:55 PM #2
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'.Last edited by kjkrum; 07122013 at 07:59 PM.
Get in the habit of using standard Java naming conventions!
 07122013, 08:33 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,206
 Rep Power
 9
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:
Java 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); }
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Writing a program that adds 2 rational numbers (in the form of fractions) in BlueJ.
By dangolf in forum New To JavaReplies: 2Last Post: 02082013, 01:46 PM 
IBM Rational
By takugian in forum Forum LobbyReplies: 0Last Post: 10012012, 09:13 PM 
Rational Clearcase
By Rajavardhan in forum New To JavaReplies: 1Last Post: 07282012, 08:23 AM 
trying to understand this rational numbers class
By sonny in forum New To JavaReplies: 9Last Post: 03282010, 10:29 PM 
Rational numbers  problem with comparsions
By omygoodness in forum New To JavaReplies: 9Last Post: 01032010, 09:20 PM
Bookmarks