help with rational assignment

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!!


Comments

  • Hi,
    Thanks for sharing your ideas and views with us. I would like to know more on this.

    CSK

  • just wondering if u did get help.. i have the same program and have no clue how to do it!
  • 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);
    }
    }


    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories