## Java

Number of posts: 18218

This Forum Only

help with rational assignment Posted by exodus1386 on 12 Mar 2009 at 2:13 PM
heres the assignment:

write and fully test a class that represents rational numbers. A rational number can be represented as the ration of two integer values, a and b, where b is not zero. The class has attributes for the numerator and denominator of this ration. The ratio should always be stored in its simplest form.

The class has the following constructors and methods:
-a default constructor that sets rational number to 0/1

-a constructor that has parameters for numerator and denominator, and converts to resulting ratio in simplified form

-simplify-------a private method that converts the rational number to simplified form

-getGCD(x,y)-----a private static method that returns the largest common factor of two positive integers x and y, their greatest common divisor.

-getValue---returns the rational number as a double value

-toString---returns the rational number as a string in the form a/b

okay so up to this point i have understand what i was doing and got 100 percent on all my assignments but i got to this one and im completely lost. ive seen alot of rational classes that did all the arithmetic but as from what i can see i need something completely different. i need some serious help as its due mon night 17th and im lost.

thanx ahead of time for anyone willing to help!!

Re: help with rational assignment Posted by CSK001 on 14 Mar 2009 at 3:13 AM
Hi,
Thanks for sharing your ideas and views with us. I would like to know more on this.

CSK

Re: help with rational assignment Posted by bottle_pop on 3 May 2009 at 10:16 PM
just wondering if u did get help.. i have the same program and have no clue how to do it!
Re: help with rational assignment Posted by djgalore on 11 May 2009 at 12:31 AM
From the top of my head :

public class Test{

public Test(int a, int b){
this.a = a;
this.b = b;
simplify();

}

public void simplify(){
int gcd = getGCD(a, b);
a = a / gcd;
b = b / gcd;
}

public double getValue(){
return a/b;
}

public String toString(){
return a + "/" + b;
}

public int getGCD(int a, int b){
if (b == 0){
return a;
}
else{
return getGCD(b, a % b);
}
}

}

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic