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

# Thread: Complex numbers need help finishing

1. ## 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)
{

}

}```  Reply With Quote

3. ## Re: Complex numbers need help finishing

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

4. ## Re: Complex numbers need help finishing 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) 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.  Reply With Quote

5. ## Re: Complex numbers need help finishing

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