# Thread: Rational - greatest common devisor help.

1. Member Join Date
May 2013
Posts
11
Rep Power
0

## Rational - greatest common devisor help.

Hello, I am doing a rational class where I do arithmetic operations with fractions. Everything works correctly, but the problem I am having is simplifying the fraction(starts line 54 to 67). So if I input 1/2 - 1/2, it would display "0/4" & 1/2 + 1/2 = 2/2. So here is my code and test code:
Java 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;
}
[B]private int gcd(int numerator, int denominator)
{
int r;
while(denominator!= 0)
{
r = numerator % denominator;
numerator = denominator;
denominator = r;

}
return numerator;
}[/B]

{
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;
}
}```
Java 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();

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());
}

}```
Therefore, any help on that issue is greatly appreciated.  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rational - greatest common devisor help.

You need to reverse the roles of denominator and numerator. To calculate GCD it is r = largest % smallest.
Then numerator = r and denominator = smallest.

For proper fractions, the denominator is the largest. For improper fractions, the numerator is larger. If you want to include both,
you need to swap their roles.

take 28/35 or 35/28

35 % 28 = 7
28 % 7 = 0

So 7 is the GCD and the fraction reduced to 4/5 or 5/4.

Regards,
Jim  Reply With Quote

3. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rational - greatest common devisor help.

Please ignore my previous (and ill thought out) previous post. I would edit it but I can't. Grumble.

Regards,
Jim  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rational - greatest common devisor help.

I am not certain what the problem is with your code. To simplify fractions, you need to divide the numerator and denominator by the gcd. Do you do that? Also, do you want to leave 0/4 as 0/4 or 0? Do you want to leave 99/99 as 99/99 or 1?

Regards,
Jim  Reply With Quote

5. Member Join Date
May 2013
Posts
11
Rep Power
0

## Re: Rational - greatest common devisor help.

Hey thanks for the replies. I want to make sure if the answer is 0/4, the output would be 0, and if 99/99, the output to be 1. Still working on the code.  Reply With Quote

6. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rational - greatest common devisor help.

Well, I presume a couple of if statements would do the trick.

Regards,
Jim  Reply With Quote

7. Member Join Date
May 2013
Posts
11
Rep Power
0

## Re: Rational - greatest common devisor help. Originally Posted by jim829 Well, I presume a couple of if statements would do the trick.

Regards,
Jim
Yes, I have worked on it and manage to simplify the division and subtraction well.

int divisor = gcd(numerator, denominator);
numerator = numerator/divisor;
denominator = denominator/divisor;
Added on each of the arithmetic.
but it does not seem to work with add/mult. Anywho, still working on this part, and once I finish and find whats wrong with add/mult simplifying, I should be all finished.  Reply With Quote

8. ## Re: Rational - greatest common devisor help.

If you follow a few simple rules, all will be easy:

2) do the gcd( ... ) trick in the constructor;
3) for negative numbers make the numerator negative (the denominator will be positive);
4) don't print the part "/denominator" if the denominator == 1.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•