Adding Fractions Problem  
Author Message
Rowan B





PostPosted: 2005-2-5 3:47:00 Top

java-programmer, Adding Fractions Problem I can use the code below to add fractions together through adding instances
of the class Rational together, but my code to reduce the result to its
lowest terms has no effect. Why is this?

public class Rational
{
// instance variables - replace the example below with your own
private int numerator;
private int denominator;

public Rational()
{
}

public Rational(int num, int denom)
{
numerator = num;
denominator = denom;
}

public int getNumerator()
{
return numerator;
}

public int getDenominator()
{
return denominator;
}

public Rational add(Rational rhs)
{
int numerator1 = (rhs.getDenominator() * numerator);
int newDenom = (rhs.getDenominator() * denominator);
int numerator2 = (denominator * rhs.getNumerator());
int newNum = (numerator1 + numerator2);
if((newDenom % newNum) == 0)
{

// This code is supposed to reduce the result to its lowest
terms

int commonFactor = (newDenom / newNum);
newDenom /= commonFactor;
newNum /= commonFactor;
}
return new Rational(newNum, newDenom);
}
}


 
Eric Sosman





PostPosted: 2005-2-5 4:43:00 Top

java-programmer >> Adding Fractions Problem

Rowan B wrote:
> I can use the code below to add fractions together through adding instances
> of the class Rational together, but my code to reduce the result to its
> lowest terms has no effect. Why is this?
> [...]
> if((newDenom % newNum) == 0)
> {
>
> // This code is supposed to reduce the result to its lowest
> terms
>
> int commonFactor = (newDenom / newNum);
> newDenom /= commonFactor;
> newNum /= commonFactor;

It's because you don't understand how to reduce to
lowest terms. Consider a few test cases:

12 / 6 -- your code handles this one correctly

12 / 1 -- your code handles this one correctly,
but does more work than it needs to

12 / 9 -- your code leaves this one unchanged,
but it shouldn't. Quick: What's `12 % 9', and
why is it the wrong thing to calculate? More to
the point, what is the *right* thing to calculate?

Since there's a homework-ish aura hovering around your
question, I'll limit myself to providing these hints and
not offering code.

--
email***@***.com

 
Fran鏾is Grondin





PostPosted: 2005-2-5 4:53:00 Top

java-programmer >> Adding Fractions Problem The condition (newDenom % newNum) == 0 is not the good one. For example,
5/12 + 1/3 = 9/12 = 3/4. And in that case, 12 % 9 = 3 !=0.

Sadly, you may have to add an algorithm to find the greatest common divisor
of two integers. In the previous example, GCD(9, 12) = 3, and 9/12 = (9/3) /
(12/3) = 3/4, which is the desired result.

Search for "greatest common divisor" on Google. It may help. Good luck.

Fran鏾is


"Rowan B" <email***@***.com> wrote in message
news:bjQMd.1145$email***@***.com...
> I can use the code below to add fractions together through adding
instances
> of the class Rational together, but my code to reduce the result to its
> lowest terms has no effect. Why is this?
>
> public class Rational
> {
> // instance variables - replace the example below with your own
> private int numerator;
> private int denominator;
>
> public Rational()
> {
> }
>
> public Rational(int num, int denom)
> {
> numerator = num;
> denominator = denom;
> }
>
> public int getNumerator()
> {
> return numerator;
> }
>
> public int getDenominator()
> {
> return denominator;
> }
>
> public Rational add(Rational rhs)
> {
> int numerator1 = (rhs.getDenominator() * numerator);
> int newDenom = (rhs.getDenominator() * denominator);
> int numerator2 = (denominator * rhs.getNumerator());
> int newNum = (numerator1 + numerator2);
> if((newDenom % newNum) == 0)
> {
>
> // This code is supposed to reduce the result to its lowest
> terms
>
> int commonFactor = (newDenom / newNum);
> newDenom /= commonFactor;
> newNum /= commonFactor;
> }
> return new Rational(newNum, newDenom);
> }
> }
>
>


 
 
Rowan B





PostPosted: 2005-2-5 5:45:00 Top

java-programmer >> Adding Fractions Problem
"Fran鏾is Grondin" <email***@***.com> wrote in message
news:bhRMd.1918$3j4.630@edtnps89...
> The condition (newDenom % newNum) == 0 is not the good one. For example,
> 5/12 + 1/3 = 9/12 = 3/4. And in that case, 12 % 9 = 3 !=0.
>
> Sadly, you may have to add an algorithm to find the greatest common
divisor
> of two integers. In the previous example, GCD(9, 12) = 3, and 9/12 = (9/3)
/
> (12/3) = 3/4, which is the desired result.
>
> Search for "greatest common divisor" on Google. It may help. Good luck.
>
> Fran鏾is


I knew it wouldn't work for that, but it won't even reduce 4/8.


 
 
klynn47





PostPosted: 2005-2-5 6:18:00 Top

java-programmer >> Adding Fractions Problem I think the problem is as mentioned. Instead of testing to see if the
numerator is a divisor of the denominator, you should check to see if
they have any factors in common. The algorithm for finding the gcd of
two numbers is very simple.

Create a variable called remainder set equal to 1
while (remainder > 0)
Let remainder = the remainder of b divided by a
Let b = a
Let a = remainder

In this case, after the loop is finished, b is the greatest common
divisor.

Once you know that the greatest common divisor of the numbers is
greater than 1, then just divide numerator and denominator by the
greatest common divisor

 
 
Thufir Hawat





PostPosted: 2005-2-5 8:21:00 Top

java-programmer >> Adding Fractions Problem In general, it helps to use System.out.println() to help figure this
sort of thing out, and to hard-code your examples until you've got
things working.