Need a hand with some method logic here, I got all the code written but I am having an issue trying to get this logic to work. These two methods are designed to simplify fractions and convert improper fractions to mixed numbers ( two seperate methods). Take a look and let me know what you guys think. Thanks.

-Sky
Also, I am getting a compile error in the last method (simplifyFractions), compiler complains about a "unresolved symbol for the variable newdenomerator".
Cant seem to figure out what the deal is, I think it may have something to do with my return type being a string and newdenomerator being an int. GAH!

public String mixedFractionValue(int numerator, int denomerator)    //Method to convert improper fraction to mixed number BEGIN
    {
        int wholeNumber = ( numerator / denomerator );
        int partRemainder = ( numerator % denomerator );
        return ("" + wholeNumber + partRemainder + "/" + denomerator );
    }       //End mixedFractionValue method

    public String simplifyFractions ( int numerator, int denomerator)   //Method to simplify fractions BEGIN
    {
        int factor = numerator;
        while ( factor > 1)
        {
            if ( denomerator % factor == 0)
            {
                int newdenomerator = ( denomerator / numerator );
            }
            factor --;
        }
        return ( "" + factor + "/" + newdenomerator );
    }       //End simplifyFractions method

Dani AI

Generated

Good catch by — the compile error comes from declaring newdenomerator inside the if block. A couple more issues to watch for (not called out yet): the code divides by numerator where it should divide by the common factor, it returns factor after the loop (but factor will have been decremented), and there’s no handling for negative values or a zero denominator. A robust approach is to compute the greatest common divisor (Euclidean algorithm), divide both numerator and denominator by it, and then format either a simplified fraction or a mixed number. Below is a compact, safe example you can drop into a class — it avoids the scoping problem, fixes the divide-by-wrong-value bug, and handles sign/zero cases.

public static int gcd(int a, int b) {
    a = Math.abs(a);
    b = Math.abs(b);
    while (b != 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

public static String simplifyFraction(int num, int den) {
    if (den == 0) throw new IllegalArgumentException("denominator must not be zero");
    if (num == 0) return "0/1";
    int g = gcd(num, den);
    num /= g; den /= g;
    if (den < 0) { num = -num; den = -den; }
    return num + "/" + den;
}

public static String mixedFractionValue(int num, int den) {
    if (den == 0) throw new IllegalArgumentException("denominator must not be zero");
    int whole = num / den;
    int rem = Math.abs(num % den);
    if (rem == 0) return Integer.toString(whole);
    int g = gcd(rem, Math.abs(den));
    rem /= g;
    int sd = Math.abs(den) / g;
    if (whole == 0) {
        int signedRem = num < 0 ? -rem : rem;
        return signedRem + "/" + sd;
    }
    return whole + " " + rem + "/" + sd;
}

Notes: fix the variable name to denominator to avoid confusion, and for programmatic use consider returning a small Fraction object instead of a String.

Recommended Answers

All 3 Replies

In your code:
...
if ( denomerator % factor == 0)
{
int newdenomerator = ( denomerator / numerator );
}
...


You delcare 'newdenomerator' within your if statement (it is declared between the { and } ).

Anything declared between brackets ({,}) has its scope only in the brackets.

Declare 'newdenomerator ' at the begining of the method, and then assign the value:

public String simplifyFractions ( int numerator, int denomerator) //Method to simplify fractions BEGIN
{
int newdenomerator = 0; // Initilized to brevent initilization error.
int factor = numerator;
while ( factor > 1)
{
if ( denomerator % factor == 0)
{
newdenomerator = ( denomerator / numerator );
}
factor --;
}
return ( "" + factor + "/" + newdenomerator );
} //End simplifyFractions method

Also, since you do an IF to determin the newdenominator, would you not want an else to set it in case if ( denomerator % factor != 0) ??

Maybe set newdenominator to denominator? For example:

if ( denomerator % factor == 0)
{
newdenomerator = ( denomerator / numerator );
} else {
newdenomerator = denomerator;
}
Otherwise the Newdenominator would be zero (since I initilize it to '0' when it was declared.)

GAH, I didnt see that. Guess I was staring at it for too long, thanks man!

hehe....NP :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.