Amazon.com Widgets Exercise 11.2- copying argument and attention to method name
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 03:07:06 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 11
| | | |-+  Exercise 11.2- copying argument and attention to method name
Pages: [1]   Go Down
Print
Author Topic: Exercise 11.2- copying argument and attention to method name  (Read 325 times)
eagle74
Newbie
*
Posts: 24


www.cucinareblog.it


WWW Email




« on: May 29, 2012, 02:46:49 AM »

Hy everybody,
below is my version, I copied the argument inside isEqualTo method, and I thought about a problem: if you don't declare the category where you are going to use it, it doesn't give an error when its already present in a parent class! for example isEqualTo already exists in NSObject! maybe it's superfluous but in my previous version I forgot to declare the category in main so I was using NSObject's method... Cry

Fraction+Comparison.h
Code: (Objective-C)
#import "Fraction.h"
#import "Fraction+MathOps.h"

@interface Fraction (Comparison)

-(BOOL) isEqualTo: (Fraction *) f;
-(int) compare: (Fraction *) f;

@end

Fraction+Comparison.m
Code: (Objective-C)
Fraction *newF;

-(BOOL) isEqualTo: (Fraction *) f
{//copying argument to avoid changing its original value
    {if (!newF)
        newF = [[Fraction alloc] init];
        newF.numerator=f.numerator;
        newF.denominator=f.denominator;
    }
    [self reduce];
    [newF reduce];
    if ([self numerator] == [newF numerator] &&
        [self denominator] == [newF denominator])
    return YES;
    else return NO;
   
}

-(int) compare: (Fraction *) f
{
   
    newF = [self sub: f];
   
    if((newF.numerator <0) || (newF.denominator<0) )
        return -1;
    else if(newF.numerator ==0)
        return 0;
    else if((newF.numerator >0) && (newF.denominator>0))
        return 1;
    else return 999;

}

@end

main.m
Code: (Objective-C)
#import "Fraction.h"
#import "Complex.h"
#import "Fraction+MathOps.h"
#import "Fraction+Comparison.h"

int main (int argc, char *argv[])
{
    //id c1 = [[Complex alloc] init];
    Fraction *a = [[Fraction alloc] init];
    Fraction *b = [[Fraction alloc] init];
    Fraction *result;
    [a setTo: 1 over: 4]; [b setTo: 1 over: 2];
    [a printH];
    [a print];
    NSLog (@" +");
    [b print];
    NSLog (@"-----");
    result = [a add: b];
    [result print];
    NSLog (@"\n");

    [a print];
    NSLog (@" -");
    [b print];
    NSLog (@"-----");
    result = [a sub: b];
    [result print];
    NSLog (@"\n");
   
    [a print]; NSLog (@" *"); [b print]; NSLog (@"-----"); result = [a mul: b];
    [result print];
    NSLog (@"\n");
   
    [a print]; NSLog (@" /"); [b print]; NSLog (@"-----"); result = [a div: b];
    [result print];
    NSLog (@"\n");
    NSLog(@"L'inverso di quest'ultima frazione è ");
    [[result invert] print];
   
    if ([a respondsToSelector: @selector (printH)] == YES)
    {
        NSLog(@"conforme ");
    }
       if ([a isEqualTo: b])
    {
        NSLog(@"Le due frazioni ");
        [a print];
        NSLog(@"e");
        [b print];
        NSLog(@"sono uguali" );
    }
    [a setTo: 1 over: 8]; [b setTo: 1 over: 4];
     
    NSLog(@"Il confronto ritorna %i", [a compare: b]);
   
    return 0;

}
Logged

"The more i know people the more i love dogs."Socrate
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.