# Complex numbers need help finishing

• July 12th, 2013, 01:54 AM
therealvasile
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

Code :

```  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) {   }   }```
• August 1st, 2013, 11:40 AM
llowe29
Re: Complex numbers need help finishing
you cant call reduce method before creating it . organization matters!
• August 1st, 2013, 12:41 PM
jps
Re: Complex numbers need help finishing
Quote:

Originally Posted by therealvasile
(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
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.
• August 1st, 2013, 01:26 PM
llowe29
Re: Complex numbers need help finishing
That is weird because Ive thougt that was the problem in some of my programs, but thanks anyway.