function question about Objective - C code from Kochan book

Discussion in 'Mac Programming' started by theotherbeck, Nov 18, 2008.

  1. theotherbeck macrumors newbie

    Joined:
    Nov 15, 2008
    Location:
    New York, NY
    #1
    Hi all,

    I've been going through Kochan's objectivive c book, and I ran into some code that I have a quick question about. In the code below, there is a reduce function that reduces a fraction. I am currently calling it from inside the add function. In an earlier book example, we did the same thing and it reduced the fraction without trouble... in fact, that was the point of the example... to show that that worked. Then we added a few things (the new resultFraction object mostly) in the program section, and now when I use the add function, the fraction is not reduced. If I call the reduce function from the program section of the code, then it works fine, but I would like to know why it isn't working from inside the add function anymore. Anyone have any thoughts? here is the code... it is code right from the book, so things are meant to be done the way they are done for this example (however, it was broken up into 3 different files... I hope I have not missed anything when I put them together for this posting). Thanks in advance for your thoughts!

    Beck

    Code:
    #import <objc/Object.h>
    #import <stdio.h>
    
    //--------------------@interface section
    
    // Define the Fraction class
    
    @interface Fraction : Object
    {
    	int		numerator;
    	int 	denominator;
    }
    
    -(void)		print;
    -(void)		setNumerator: (int) n;
    -(void)		setDenominator: (int) d;
    -(void)		setTo: (int) n over: (int) d;
    -(int)		numerator;
    -(int)		denominator;
    -(double)	convertToNum;
    -(Fraction *)		add: (Fraction *) f;
    -(void)		reduce;
    	
    @end
    
    //----------@implementation section
    
    
    
    @implementation Fraction;
    -(void) print 
    {
    	printf (" %i/%i ", numerator, denominator);
    }
    
    -(void) setNumerator: (int) n
    {
    	numerator = n;
    }
    
    -(void) setDenominator: (int) d
    {
    	denominator = d;
    }
    
    -(void)		setTo: (int) n over: (int) d
    {
    	numerator = n;
    	denominator = d;
    }
    
    -(int) numerator 
    {
    	return numerator;
    }
    
    -(int) denominator
    {
    	return denominator;
    }
    
    -(double) convertToNum
    {
    	if (denominator != 0)
    		return (double) numerator / denominator;
    	else
    		return 1.0;
    }
    
    
    -(Fraction *)		add: (Fraction *) f
    {
    	//to add two fraction together using
    	//a/b + c/d = ((a*d) + (b*c)) / (b*d)  yikes!  ****ing math.
    	
    	Fraction *result = [Fraction new];
    	int resultNum, resultDenom;
    	
    	resultNum = (numerator * [f denominator]) + (denominator * [f numerator]);
    	resultDenom = denominator * [f denominator];
    	
    	
    	[result setTo:resultNum over:resultDenom];
    	
    	[self reduce];
    	
    	return result;
    }
    
    -(void)		reduce
    {
    	int u = numerator;
    	int v = denominator;
    	int temp;
    	
    	while (v!=0) {
    		temp = u % v;
    		u = v;
    		v = temp;
    	}
    	
    	numerator /= u;
    	denominator /= u;
    }
    
    @end
    //--------------------program section
    
    int main (int argc, char *argv[])
    {
    	Fraction *aFraction = [[Fraction alloc] init]; 
    	Fraction *bFraction = [[Fraction alloc] init]; 
    	
    	Fraction *resultFraction;
    
    //set the fractions to be used
    	
    	[aFraction setTo: 1 over: 4];
    	[bFraction setTo: 1 over: 2];
    
    //  add result
    	
    	[aFraction print];
    	printf(" + ");
    	[bFraction print];
    	printf(" = ");
    	
    //fraction is reduced in the add function
    	
    	resultFraction = [aFraction add: bFraction];
    	[resultFraction print];
    	printf ("\n");
    		
    	[aFraction free];
    	[bFraction free];
    	[resultFraction free];
    	
    	return 0;
    }
    
     
  2. theotherbeck thread starter macrumors newbie

    Joined:
    Nov 15, 2008
    Location:
    New York, NY
    #2
    got it!

    never mind! I think I figured it out! :) (simple pleasures)

    I think the answer is that I had said [self reduce];, and once the code changed and there was a new result function nested inside the add function, and the result function was the one being used and being passed back into the program, I was still effectively reducing add... not result.... since self refers to add in this case. I changed the self to result as in [result reduce]; and that worked!

    Fun! (I'm new to programming, so I still am very much in the getting-excited-when-I-make-sense-of-something stage). i hope i was right about this one... the program ran without errors, though, so I think I am.

    Beck
     
  3. theotherbeck thread starter macrumors newbie

    Joined:
    Nov 15, 2008
    Location:
    New York, NY
    #3
    funny / stupid

    So it turns out that I am right!! How do I know? That is what the code in the book said in the first place... I just didn't change it in my code. I think I get an A for figuring it out, but an F for paying attention in the first place.

    :rolleyes:

    Beck
     

Share This Page