# product of fractions

• 01-29-2014, 12:17 PM
tonyboy00
product of fractions
Hi to all ,sorry fo my bad english. I have the following code Java (is it just a meaningless example ), my question is: if I wanted to insert a code to multiply two fraction into moltiplicaPer() metod What should I write? Thx in advance. Antonio

public class Frazione {

private int num;
private int den;

Frazione() {
num = 0;
den = 1;
}

Frazione(int a, int b) {
num = a;
den = b;

}

public void moltiplicaPer(Frazione f2) {

//insert code here...
}

}//end class
• 01-29-2014, 12:44 PM
JosAH
Re: product of fractions
Well, mathematically the product of two fractions n1/d1 and n2/d2 is (n1*n2)/(d1*d2); it won't be much different in Java code ...

kind regards,

Jos
• 01-29-2014, 04:19 PM
tonyboy00
Re: product of fractions
Thanks, but I wanted this mean:

public void moltiplicaPer(Frazione f2){
Frazione r = new Frazione();
r.num = f2.num * num;
r.den = f2.den * den;
}
• 01-29-2014, 05:17 PM
JosAH
Re: product of fractions
Quote:

Originally Posted by tonyboy00
Thanks, but I wanted this mean:

Code:

```public void moltiplicaPer(Frazione f2){       Frazione r = new Frazione();       r.num = f2.num * num;       r.den = f2.den * den;     }```

Yep, that's close but you should return the new Frazione r and maybe simplify the numerator and denominator.

kind regards,

Jos