# Rational - GCD issue

• September 4th, 2013, 02:45 PM
kay19
Rational - GCD issue
Hello, I am having trouble trying to build the "gcd(Greatest common divisor". It's where if I input "1/2 - 3/4" output would be "-2/8", where I would want "-1/4". Here is my code. I finished all my arithmetic's, and now all I need to do is simplify my answers. After that, I should be good with this code. Any help is appreciated.

Code :

```import java.util.Scanner;     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 static 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, Rational f2) { numerator = (f1.numerator * f2.denominator) - (f2.numerator * f1.denominator); denominator = f1.denominator * f2.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's my tester.
Code :

```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,f2); 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()); }   }```
• September 4th, 2013, 03:13 PM
GregBrannon
Re: Rational - GCD issue
This is a time when posting an example run would be helpful to explain what you're asking for help with. So when I run your code for the fractions you suggest, I get:
Code :

```Enter numerator 1 Enter Denominator 2 Enter numerator 3 Enter Denominator 4 1/2 + 3/4 = 10/8 1/2 - 3/4 = -2/8 1/2 * 3/4 = 3/8 1/2 / 3/4 = 4/6 0.0 Numerator: 2 Denominator: 5```
Are you asking for help to simplify all of the resulting fractions, 10/8, -2/8, 3/8, and 4/6?

What are the results 0.0, Numerator: 2 and Denominator: 5 supposed to be? Are those right?

I notice that you never use the method gcd(). Why is that?
• September 4th, 2013, 09:14 PM
kay19
Re: Rational - GCD issue
Quote:

Originally Posted by GregBrannon
This is a time when posting an example run would be helpful to explain what you're asking for help with. So when I run your code for the fractions you suggest, I get:
Code :

```Enter numerator 1 Enter Denominator 2 Enter numerator 3 Enter Denominator 4 1/2 + 3/4 = 10/8 1/2 - 3/4 = -2/8 1/2 * 3/4 = 3/8 1/2 / 3/4 = 4/6 0.0 Numerator: 2 Denominator: 5```
Are you asking for help to simplify all of the resulting fractions, 10/8, -2/8, 3/8, and 4/6?

What are the results 0.0, Numerator: 2 and Denominator: 5 supposed to be? Are those right?

I notice that you never use the method gcd(). Why is that?

Thanks for the reply. Yes, I was missing the method gcd. Here is what i've done.

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

^this should be posted right after the add/sub/mul/div parts. It works for Sub/Div.

if (numerator == 0){
return "0";
}
else if (numerator == denominator){
return "1";

^I put this in the ToString method. So, yeah, just figuring out whats wrong with simplifying the addition & Multiplication, and i'm finish with this work.
• September 5th, 2013, 03:38 AM
syedbhai
Re: Rational - GCD issue
Hello.
You are still fine.
Suppose your output is n/m. You can find the common factors for n and m one by one. If a factor say 'f' is found then reduce both n and m by f. With this you can avoid GCD.

Syed.