These are the current codes for my addition subtraction methods that work perfectly fine:
public static Rational sub(Rational r1, Rational r2){
int a = r1.getNum();
int b = r1.getDenom();
int c = r2.getNum();
int d = r2.getDenom();
int numForNow = a*d - b*c;
int denomForNow = b*d;
Rational ratNum = new Rational (numForNow, denomForNow);
return ratNum;
public static Rational add(Rational r1, Rational r2){
int numForNow = r1.getNum()*r2.getDenom() + r2.getNum() * r1.getDenom();
int denomForNow = r1.getDenom() * r2.getDenom();
Rational ratNum = new Rational (numForNow, denomForNow);
return ratNum;
}
So if I add two rationals like 1/3 and 4/6 I would get 18/18 (reduces to 1). However, I want to write these in a different way so that the program sees that 3 goes into 6 and would just print out 6/6.
I know I would take the LCM for the denominator, which I understand. I don't understand how to make it so that the numerator would follow suit?
Also, I think there would need to be an if statement to determine whether or not to use the LCM or just continue using the code already there.