Reputation: 11
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.
Upvotes: 0
Views: 5889
Reputation: 134
If you want, here a little Rational-class:
package snippets;
public class Rational {
private final long num;
private final long denom;
private volatile int hashCode;
public Rational(long num, long denom) {
this.num = num;
this.denom = denom;
}
public long getNum() {
return num;
}
public long getDenom() {
return denom;
}
public Rational add(Rational other) {
long numForNow = num * other.denom + other.num * denom;
long denomForNow = denom * other.denom;
return new Rational(numForNow, denomForNow).cancel();
}
public Object sub(Rational other) {
long numForNow = num * other.denom - denom * other.num;
long denomForNow = denom * other.denom;
return new Rational(numForNow, denomForNow).cancel();
}
public Rational cancel() {
long gcd = gcd(num, denom);
return new Rational(num / gcd, denom / gcd);
}
// greatest common divisor
long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Rational))
return false;
Rational other = (Rational) obj;
return num == other.num && denom == other.denom;
}
@Override
public int hashCode() {
if (hashCode == 0) {
int result = 17;
result = 31 * Long.valueOf(num).hashCode();
result = 31 * Long.valueOf(denom).hashCode();
hashCode = result;
}
return hashCode;
}
@Override
public String toString() {
return num + "/" + denom;
}
}
Tested with:
package snippets;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class RationalTest {
@Test
public void testCancel() throws Exception {
assertEquals(new Rational(2, 1), new Rational(6, 3).cancel());
assertEquals(new Rational(7, 8), new Rational(7, 8).cancel());
}
@Test
public void testAdd() {
assertEquals(new Rational(2, 1), new Rational(2, 3).add(new Rational(4, 3)));
assertEquals(new Rational(51, 40), new Rational(2, 5).add(new Rational(7, 8)));
}
@Test
public void testSub() throws Exception {
assertEquals(new Rational(2, 3), new Rational(4, 3).sub(new Rational(2, 3)));
assertEquals(new Rational(19, 40), new Rational(7, 8).sub(new Rational(2, 5)));
}
}
Upvotes: 0
Reputation: 45725
See this answer: simplifying fractions in Java
public static long gcm(long a, long b) {
return b == 0 ? a : gcm(b, a % b); // Not bad for one line of code :)
}
public static String asFraction(long a, long b) {
long gcm = gcm(a, b);
return (a / gcm) + "/" + (b / gcm);
}
Or in your case you might want a function named normalize
that gets a Rational
and returns a new Rational
normalized using the same logic as the asFraction
function above. And another asString
function that prints a Rational
as string.
On a side note, unless this is your specific requirements, I would rather have the methods as members of class Rational
and not as static methods.
Upvotes: 2
Reputation: 19278
int numberYouWant = 0;
int start = Math.max(r1, r2);
for(int i = 0; i<(r1*r2); i++){
if((i%r1==0)&&(i%r2==0)){
numberYouWant = i;
return;
}
}
I think something like this is what you want. It returns the first number by which r1 and r2 are dividable.
Upvotes: 0