# Thread: How to reduce fractions in Java?

1. Member Join Date
Apr 2012
Posts
3
Rep Power
0

## How to reduce fractions in Java?

I'm new to these forums and I'm not sure if this is the right place to post this question, I hope that's alright. I'm writing a code for my intro to Java Class that manipulated fractions through the different opertions (add, subtr, mult, divide) I have everything done but I am stuck when it comes to reducing fractions I have absolutely no idea what I am supposed to do. My professor gave us this code:

Java Code:
``` private int gcf (int a , int b ) {
return a%b == 0 ? b : gcf ( b , a%b );
}```
and he says we have to invoke it to simplify the fractions but I have no idea how to incorporate it into my code.

Here is my code (please excuse it's noobiness, I'm still learning):
Class 1:

Java Code:
```public class Fraction {

private int numerator;
private int denominator;

//constructor for building fractions
public Fraction(int num, int denom){
numerator = num;
denominator = denom;
}
//simplifying fractions
private int gcf (int a , int b ) {
return a%b == 0 ? b : gcf ( b , a%b );
}

public static Fraction add(Fraction F1, Fraction F2){
if(F1.denominator==0 || F2.denominator==0){
System.out.println("Undefined, you cannot divide by zero.");
}
if((F1.numerator*F2.denominator + F1.denominator*F2.numerator)==0 || (F1.numerator+F2.numerator)==0){
System.out.println("Sum is: 0");
}
if(F1.denominator!=F2.denominator)
return new Fraction((F1.numerator*F2.denominator + F1.denominator*F2.numerator),(F1.denominator*F2.denominator));
else if (F1.denominator==F2.denominator)
return new Fraction((F1.numerator+F2.numerator),(F1.denominator));
return null;
}
//subtraction
public static Fraction subtract(Fraction F1, Fraction F2){
if(F1.denominator==0 || F2.denominator==0){
System.out.println("Undefined, you cannot divide by zero.");
}
if((F1.numerator*F2.denominator - F1.denominator*F2.numerator)==0 || (F1.numerator-F2.numerator)==0){

}
if(F1.denominator!=F2.denominator)
return new Fraction((F1.numerator*F2.denominator - F1.denominator*F2.numerator),(F1.denominator*F2.denominator));
else if (F1.denominator==F2.denominator)
return new Fraction((F1.numerator-F2.numerator),(F1.denominator));
return null;
}
//multiply
public static Fraction multiply(Fraction F1, Fraction F2){
if(F1.denominator==0 || F2.denominator==0){
System.out.println("Undefined, you cannot divide by zero.");
}
return new Fraction((F1.numerator*F2.numerator),(F1.denominator*F2.denominator));
}
//divide
public static Fraction divide(Fraction F1, Fraction F2){
return new Fraction((F1.numerator*F2.denominator),(F1.denominator*F2.numerator));
}
//comparing fraction

public static Fraction compare(Fraction F1, Fraction F2){
if((F2.denominator*F1.numerator) > (F2.numerator*F1.denominator)){
System.out.println("Comparing Fraction 1 & Fraction 2: " + F1.numerator+"/"+F1.denominator + ">"+
F2.numerator+ "/"+ F2.denominator);
}
else if((F2.denominator*F1.numerator) < (F2.numerator*F1.denominator)){
System.out.println("Comparing Fraction 1 & Fraction 2: " + F1.numerator+"/"+F1.denominator + "<"+
F2.numerator+ "/"+ F2.denominator);
}
else if((F2.denominator*F1.numerator) == (F2.numerator*F1.denominator)){
System.out.println("Comparing Fraction 1 & Fraction 2: " + F1.numerator+"/"+F1.denominator + "="+
F2.numerator+ "/"+ F2.denominator);
}
return null;

}

//printing the fraction
public String toString(){
return  numerator + "/" + denominator ;
}

}```

Class 2:

Java Code:
```public class TestFraction {

public static void main ( String[] args ){

//fraction code (num, den)
Fraction fr1 = new Fraction((int)(Math.random()*10), (int) (Math.random()*10)+1);
Fraction fr2 = new Fraction((int)(Math.random()*10), (int) (Math.random()*10)+1);

//print the fractions in multiple forms
System.out.println("Fraction 1: " + fr1 + "\t\tFraction 2: " + fr2);

System.out.println("Sum is: " + Fraction.add(fr1, fr2));
System.out.println("Difference is: " + Fraction.subtract(fr1,fr2));
System.out.println("Product is: " + Fraction.multiply(fr1, fr2));
System.out.println("Quotient is: " + Fraction.divide(fr1, fr2));
System.out.print(Fraction.compare(fr1, fr2));
}

}```  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: How to reduce fractions in Java?

gcf stands for greatest common factor. Eg the gcf of 24 and 16 is 8. Given a fraction like n/m we reduce it by diving the top and bottom by the gcd of n and m. Eg 16/24 becomes 2/3 after diving by 8.  Reply With Quote

3. Member Join Date
Apr 2012
Posts
3
Rep Power
0

## Re: How to reduce fractions in Java?

I don't understand how to write the gcd divided by numerator and denominator. Every time I try, it comes out to an error or it makes my two random fractions the same fraction. I don't know what I'm doing wrong, a lot of my code related to gcf ends up being guess work and stabs in the dark, hoping it will work out eventually.  Reply With Quote

4. ## Re: How to reduce fractions in Java?

If you're doing things right (i.e. make your Fractions immutable) you only have to use the gcf( ... ) method in the constructor, i.e.

Java Code:
```public Fraction(int n, int d) {
int gcf= gcf(n, d);

this.n= n/gcf;
this.d= d/gcf;
}```
kind regards,

Jos  Reply With Quote

5. Member Join Date
Apr 2012
Posts
44
Rep Power
0

## Re: How to reduce fractions in Java?

I am a beginner. This might help
Java Code:
```public class Main{

public static void main(String[] args){

Fraction f1=new Fraction(3,8);
Fraction f2=new Fraction(5,6);

}

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

public static Fraction add(Fraction f1,Fraction f2){

Fraction result=new Fraction(0,1);

int num=f1.num*f2.denom+f2.num*f1.denom;
int denom=f1.denom*f2.denom;

int x=gfc(num,denom);

result.num=num/x;
result.denom=denom/x;

return result;
}
}
class Fraction{

public int num;
public int denom;

Fraction(int n,int d){
num=n;
denom=d;
}

public String toString(){
return num+"/"+denom;
}
}```  Reply With Quote

6. ## Re: How to reduce fractions in Java?

kind regards,

Jos  Reply With Quote

7. Member Join Date
Apr 2012
Posts
44
Rep Power
0

## Re: How to reduce fractions in Java?

@JosAH
" you only have to use the gcf( ... ) method in the constructor"
Java Code:
```
public static Fraction add(Fraction f1,Fraction f2){

int num=f1.num*f2.denom+f2.num*f1.denom;
int denom=f1.denom*f2.denom;

return new Fraction(num,denom);
}```
I do not understand this "i.e. make your Fractions immutable" . What do you mean?
Τhank you very much.  Reply With Quote

8. ## Re: How to reduce fractions in Java? Originally Posted by kosmos890 @JosAH
" you only have to use the gcf( ... ) method in the constructor"
Java Code:
```
public static Fraction add(Fraction f1,Fraction f2){

int num=f1.num*f2.denom+f2.num*f1.denom;
int denom=f1.denom*f2.denom;

return new Fraction(num,denom);
}```
I do not understand this "i.e. make your Fractions immutable" . What do you mean?
Τhank you very much.
Never change the member variables n and d; a fraction is a type of number and numbers don't change their value.

kind regards,

Jos  Reply With Quote

9. Member Join Date
Apr 2012
Posts
3
Rep Power
0

## Re: How to reduce fractions in Java? Originally Posted by JosAH If you're doing things right (i.e. make your Fractions immutable) you only have to use the gcf( ... ) method in the constructor, i.e.

Java Code:
```public Fraction(int n, int d) {
int gcf= gcf(n, d);

this.n= n/gcf;
this.d= d/gcf;
}```
kind regards,

Jos

I don't know what to do with this since my fractions are randomly generated using Math.random(
Last edited by uselessparadigm; 04-09-2012 at 07:00 PM.  Reply With Quote

10. ## Re: How to reduce fractions in Java? Originally Posted by uselessparadigm I don't know what to do with this since my fractions are randomly generated using Math.random(
Fine; something randomly generates a numerator and a denominator and then a Fraction object is generated; in the constructor of your Fraction you apply the logic I outlined in my previous reply.

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
•