My main.java

public static void main(String[] args) {
        // TODO code application logic here
        Rational x = new Rational ();           
        x.num = 2.0;
        x.den = 3.0;

        System.out.println (x);

        Rational.negate (x);

        Rational y = new Rational (1.0, 3.0);         
        System.out.println (Rational.invert (y));
        }

My rational.java

package rational;

public class Rational {
    double num;
    double den;

    public Rational () {
        this.num = 0.0;
        this.den = 0.0;
    }

    public static void printRational (Rational r) {
        System.out.println (r.num + " / " + r.den);
    }

    public Rational (double num, double den) {
        this.num = num;
        this.den = den;
    }

    public static void negate (Rational r) {
        r.num = -r.num;
    }

    public static double invert (Rational r) {
        return Math.pow (r.num / r.den, -1);
    }

    public double toDouble () {
        return (double) num / den;
    }

    public static int gcd (int n, int d) {
        if (n < 0) {
            n = -n;
        }
        if (d < 0) {
            d = -d;
        }
        return n * (d / gcd (n, d));
    }

    public static void reduce (Rational r) {
        n / gcd (n, d);
        d / gcd (n, d);
    }

    public static double add (Rational r1, Rational r2) {
        return new Rational (add (r1.r, r2.r));
    }
   }

(a) Write a method called printRational that takes a Rational object as an argument and prints it in some reasonable format.
(b) Write a main method that creates a new object with type Rational, sets its instance variables to some values, and prints the object.
(c) Write a second constructor for your class that takes two arguments and that uses them to initalize the instance variables.
(d) Write a method called negate that reverses the sign of a rational number. This method should be a modifier, so it should return void. Add lines to main to test the new method.
(e) Write a method called invert that inverts the number by swapping the numerator and denominator. Remember the swap pattern we have seen before. Add lines to main to test the new method.
(f) Write a method called toDouble that converts the rational number to a double (floating-point number) and returns the result. This method is a pure function; it does not modify the object. As always, test the new method.
(g) Write a modifier named reduce that reduces a rational number to its lowest terms by finding the GCD of the numerator and denominator and then dividing top and bottom by the GCD. This method should be a pure function; it should not modify the instance variables of the object on which it is invoked. You may want to write a method called gcd that finds the greatest common
divisor of the numerator and the denominator.
(h) Write a method called add that takes two Rational numbers as arguments and returns a new Rational object. The return object, not surprisingly, should contain the sum of the arguments. There are several ways to add fractions. You can use any one you want, but you should make sure that the result of the operation is reduced so that the numerator and denominator have no common divisor (other than 1).


Am I on the right track here? Can somebody please check my code and tell me if I am doing everything or not? Also, I don't know how to test everything in the main method. Can anybody please give me some help there?

Thanks ;)

Instead of just posting your entire homework assignment, I would recommend doing as much as you can and then asking for help with a specific problem.

I think most people will be more likely to respond if you ask a more specific question.

Call all the methods in the main like you are doing and after each call, print the instance.

This: System.out.println (x); will not work because: When you call the System.out.println with an object as argument, the toString method of that object is called. Since you haven't defined such method, the inherited from the Object super class is called, which returns that funny looking String you see.

So either call the Rational.printRational(x); or much, much better create such method. That method will return whatever you want to see being printed:

public String toString() {
  return num + " / " + den;
}

And call it:

Rational x = new Rational ();           
        x.num = 2.0;
        x.den = 3.0;

        System.out.println ("Before: " + x);

        Rational.negate (x);

        System.out.println ("After: " + x);

The reduce method is totally wrong. You need to call the gcd with arguments the values of the Rational argument, take the result in a separate variable and use it to alter the values of the Rational argument.

In the add method you need to do some actual calculations, not just call the same method which result in the same method calling itself, which will call itself, which will call itself, which will call itself,

My Rational.java looks like this:

package rational;

public class Rational {
    double num;
    double den;

    public Rational () {
        this.num = 0.0;
        this.den = 0.0;
    }

    public static void printRational (Rational r) {
        System.out.println (r.num + " / " + r.den);
    }

    public Rational (double num, double den) {
        this.num = num;
        this.den = den;
    }

    public static void negate (Rational r) {
        r.num = -r.num;
    }

    public String toString () {
        return num + " / " + den;
    }

    public static double invert (Rational r) {
        return Math.pow (r.num / r.den, -1);
    }

    public double toDouble () {
        return (double) num / den;
    }

    public static int gcd (int n, int d) {
        if (n < 0) {
            n = -n;
        }
        if (d < 0) {
            d = -d;
        }
        return n * (d / gcd (n, d));
    }

    public Rational reduce () {
        double g = num;
        double gcd = den;
        double tmp;
        if (g < gcd) {
            tmp = g;
            g = gcd;
            gcd = tmp;
        }
        while (g != 0) {
            tmp = g;
            g = gcd % g;
            gcd = tmp;
        }
        return new Rational (num / gcd, den / gcd);
    }

    public static Rational add (Rational a, Rational b) {
        double den = a.den * b.den;
        double num = a.num * b.num;
        return new Rational (num, den);
    }
   }

My Main.java looks like this:

package rational;

public class Main {

    public static void main(String[] args) {
        Rational x = new Rational ();           
        x.num = 2.0;
        x.den = 3.0;

        System.out.println ("Before: " + x);

        Rational.negate (x);

        System.out.println ("After: " + x);

        System.out.println ();

        Rational y = new Rational (2.0, 3.0);
        System.out.println ("Invert: " + Rational.invert (y));

        System.out.println ();

        Rational z = new Rational ();
        Rational w = new Rational ();
        z.num = 5.0;
        z.den = 3.0;
        w.num = 4.0;
        w.den = 3.0;
        Rational.add (z, w);
        System.out.println ("Add: " + Rational.add (z ,w));
    }
}

I only know how to test these methods in the main method but I want to test every method that is in Rational.java. How do I do that? Please HELP :(

I only know how to test these methods in the main method but I want to test every method that is in Rational.java. How do I do that? Please HELP :(

I'm not sure I understand exactly what you're asking, but you can access the methods in a class in the same way you access a class's public members:

<class_name>.<public_method or public_member>

You can also put print statements inside the methods in your Rational class.

If you're wondering how to call the static methods then do this:

Rational num1 = new Rational( 1, 2 );
Rational num2 = new Rational( 1, 3 );
Rational num3 = Rational.add( num1, num2 );

I hope that helps.

I meant that I want to test every method in my rational.java and print the results of each method through main.java. So far, I only have invert, printRational, add and negate. I still have method named toDouble and possibly reduce to test through main.java. How do I do them?

So you want to know how to call non static methods.

Since these methods are not static, they need to be invoked on an object instance of the calling method's class.

You can do something like this:

Rational num = new Rational( 1, 2 );

System.out.println( num.toDouble() );

Its the same idea for the other methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.