Hello, well I'm doing a rational class and i'm having trouble with the subtraction part. I have done add, multiply, and division correctly. Therefore, I would like a little help on subtraction, and i'm set.

public class Rational {


        // TODO Auto-generated method stub
        //Data members
            private int numerator;
            private int denominator;
        //Constructors
        Rational()
        {
            numerator = 0;
            denominator = 1;
        }
        Rational(int num, int den)
        {
            numerator = num;
            denominator = den;
        }
    //Accessors
        public int getNumerator()
        {
            return numerator;
        }
        public int getDenominator()
        {
            return denominator;
        }
        // Modifiers
        public void setNumerator(int num)
        {
            numerator = num;
        }
        public void setDenominator(int den)
        {
            denominator = den;
        }
        public Rational inputRational(){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter numerator");
        numerator = input.nextInt();
        System.out.println("Enter Denominator");
        denominator = input.nextInt();
        return new Rational(numerator, denominator);

    }
    public String toString()
    {
        return numerator + "/" + denominator;
    }
private int gcd(int m, int n)
{
    int r;
    while(n != 0)
    {
        r = m % n;
        m = n;
        n = r;

        }
    return m;
    }
        //Adding
 public Rational add(Rational f)
 {
     int num;
     int den;
     num = (numerator * f.denominator) + (f.numerator * denominator);
     den = denominator * f.denominator;
     return new Rational(num, den);
 }
 public void sub(Rational f1)
 {
     numerator = (numerator * f1.numerator) - (f1.numerator * denominator);
     denominator = denominator * f1.denominator;

 }
 public Rational mul(Rational f)
 {
     int num;
     int den;
     num = numerator * f.numerator;
     den = denominator * f.denominator;
     return new Rational(num, den);
 }
 public void div(Rational f1, Rational f2)
 {
     this.numerator = f1.numerator * f2.denominator;
     this.denominator = f1.denominator * f2.numerator;
 }
 public static double divided(Rational f1, Rational f2)
 {
     double value;
     value = (f1.numerator/f2.denominator)*(f2.denominator/f2.numerator);
     return value;
    }
 }

Here is my test rational class.

public class testRational {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rational f1 = new Rational();
        Rational f2 = new Rational();
        Rational f3 = new Rational();
        f1.inputRational();
        f2.inputRational();

        f3 = f1.add(f2);
        System.out.println(f1.toString() + " + " + f2.toString() + " = " + f3.toString());

        f3.sub(f1);
        System.out.println(f1.toString() + " - " + f2.toString() + " = " + f3.toString());

        f3 = f1.mul(f2);
        System.out.println(f1.toString() + " * " + f2.toString() + " = " + f3.toString());

        f3.div(f1, f2);
        System.out.println(f1.toString() + " / " + f2.toString() + " = " + f3.toString());

        System.out.println(Rational.divided(f1,f2));
        f1.setNumerator(2);
        f2.setDenominator(5);
        System.out.println("Numerator: " +f1.getNumerator());
        System.out.println("Denominator: "+f2.getDenominator());
    }

}

So let's say I input 3/4 and 1/2. it prints out 3/4 - 1/2 = 6/32, which is wrong. Everything else, is correct. So anyhelp is appreciated.

It's exacty the same as addition except for the occasional minus sign instead of a plus sign. Just compare those two methods to see where sub deviates from the correct pattern in add

Comments
Yes. Exactly!

Thanks for replying. I was experimenting and I believe I did it.

     numerator = (f1.numerator * f2.denominator) - (f2.numerator * f1.denominator);
     denominator = f1.denominator * f2.denominator;

Now when I input 1/2 - 3/4, I get "-2/8". It's correct. I rather though if I could simplify it to "-1/4", but this result is fine for now :P. Unless anyone can help simplify this more, I think my assignment is complete. Well, I'll leave this topic open for couple of hours, and then resolve this topic.

To reduce the result you need to calculate gcd(numerator, denominator) simply divide the numerator and denominator by that value.

gcd = greatest common divisor

There are quite a few examples of this in Java available through a simple google search. Here is the first one i found:

public static long gcd(long a, long b) {
   return b == 0 ? a : gcd(b, a % b);
}

reference: http://stackoverflow.com/questions/6618994/simplifying-fractions-in-java

Edited 3 Years Ago by JamesCherrill: Fixed "typo"

What is wrong with this:

private int gcd(int m, int n)
{
    int r;
    while(n != 0)
    {
        r = m % n;
        m = n;
        n = r;
        }
    return m;
    }

Anywho, thanks for reply. I do indeed need to simplify this.

That should work just fine, its easier to read too. I didnt realize you had one in your origional post or i would have pointed you to it. :)

Edited 3 Years Ago by DarkLightning7

Well, the problem is that when I run the code, it does not include the GCD command, and does not simplify my answers. So i'm definetly missing something because the gcd isn't being used locally. So yeah, this is a pain in the ass, but i'm still trying to work on it.

after this:

numerator = (f1.numerator * f2.denominator) - (f2.numerator * f1.denominator);
denominator = f1.denominator * f2.denominator;

add:

int divisor = gcd(numerator, denominator);
numerator = numerator/divisor;
denominator = denominator/divisor;

similarly for any other results you need to simplify.

Comments
for helping me

Ok thanks for the help. I have mark this question solved. :)

This question has already been answered. Start a new discussion instead.