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
``````

## All 3 Replies

...
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.