Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Complex numbers need help finishing

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Complex numbers need help finishing

    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 i have so far..not sure if im even close

     
    public class Rational 
    {
        int num;
        int den;
     
        public Rational()
        {
            num = 0;
            den = 1;
        }
     
        public Rational(int a, int b)
        {
            if (b == 0)
            {
                b=1;
            }
            num = a;
            den = b;
            reduce();
     
            Rational R = new Rational(num,den);
        }
     
        public void setRational(int a, int b)
        {
            if (b == 0)
            {
                b=1;
            }
            num = a;
            den = b;
            reduce();
     
            Rational setR = new Rational(num,den);
     
        }
     
        public void setRational(int a)
        {
             a= a;
        }
     
        private void reduce()
        {
            if (num != 0)
            {
                int g = gcd(num,den);
                num = num /g;
                den = den/g;
     
            }
        }
     
        public Rational (int a)
        {
            num = a;
            den = 1;
        }
     
     
        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
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Complex numbers need help finishing

    you cant call reduce method before creating it . organization matters!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Complex numbers need help finishing

    Quote Originally Posted by therealvasile View Post
    (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 i have so far..not sure if im even close
    Try breaking the complexity down. Consider just one point at a time. Begin with (a), Do you have a constructor that accepts two integers? Yes.
    Are the two integers namd a and b respectively? Yes.
    Does this constructor create a Rational object.........

    when (a) is complete move to (b). So far looks good (did not run to test)


    Quote Originally Posted by llowe29 View Post
    you cant call reduce method before creating it . organization matters!
    Yes, you can call reduce exactly as it is in the posted code. In Java the method can be placed almost anywhere in the file, including above or below where it is called.

  4. #4
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Complex numbers need help finishing

    That is weird because Ive thougt that was the problem in some of my programs, but thanks anyway.

Similar Threads

  1. JAVA IN FINISHING SOMETHING
    By komal510 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2013, 10:47 PM
  2. some advice about building more complex programs
    By derekxec in forum Java Theory & Questions
    Replies: 2
    Last Post: July 22nd, 2012, 09:22 AM
  3. Complex Numbers in Java
    By JavaNovice03355 in forum Member Introductions
    Replies: 1
    Last Post: May 18th, 2011, 07:30 PM
  4. [SOLVED] Very complex project. It's hard to explain.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 55
    Last Post: November 4th, 2010, 11:30 AM
  5. How do you design a complex program
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2010, 06:52 PM