Results 1 to 3 of 3
  1. #1
    therealvasile is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default 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) 
        {
            
        }
    
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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; 07-12-2013 at 08:59 PM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default 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);
    }
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2013, 01:46 PM
  2. IBM Rational
    By takugian in forum Forum Lobby
    Replies: 0
    Last Post: 10-01-2012, 10:13 PM
  3. Rational Clearcase
    By Rajavardhan in forum New To Java
    Replies: 1
    Last Post: 07-28-2012, 09:23 AM
  4. trying to understand this rational numbers class
    By sonny in forum New To Java
    Replies: 9
    Last Post: 03-28-2010, 11:29 PM
  5. Rational numbers - problem with comparsions
    By omygoodness in forum New To Java
    Replies: 9
    Last Post: 01-03-2010, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •