Amazon.com Widgets Exercise 11.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 22, 2014, 12:54:54 AM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 11
| | | |-+ Exercise 11.3
Pages: [1] Go Down
Print
Author Topic: Exercise 11.3 (Read 1345 times)
tknp
Newbie
*
Posts: 23






on: January 17, 2012, 07:29:14 PM

I'm having issues adopting the protocol NSComparisonMethods to my Fractions+MathOps category. I assume I am supposed to have the line

Code: (Objective-C)
@interface Fraction (MathOps) <NSComparisonMethods>
near the top of Fractions+MathOps.h but adding the <NSComparisonMethods> to the line results in the error "Cannot find protocol declaration for 'NSComparisonMethods.' The code I have works fine but it doesn't adopt the protocol. Any tips?

Fraction+MathOps.h

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

@interface Fraction (MathOps)

//exercise 11.2
-(int)  compare: (Fraction *) f;

// exercise 11.3
-(BOOL) isEqualTo:(id)f;
-(BOOL) isLessThanOrEqualTo:(id)f;
-(BOOL) isLessThan:(id)f;
-(BOOL) isGreaterThanOrEqualTo:(id)f;
-(BOOL) isGreaterThan:(id)f;
-(BOOL) isNotEqualTo:(id)f;
@end

Fraction+MathOps.m

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

@implementation Fraction (MathOps)

// exercise 11.2
-(int) compare: (Fraction *) f
{
double a = (double) self.numerator / self.denominator;
double b = (double) f.numerator    / f.denominator;
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}

// exercise 11.3
-(BOOL) isEqualTo: (Fraction *) f
{
int result = [self compare: f];

if (result == 0)
return YES;
else
return NO;
}

-(BOOL) isNotEqualTo:(Fraction *)f
{
int result = [self compare: f];

if (result != 0)
return YES;
else
return NO;
}

-(BOOL) isLessThan:(Fraction *) f
{
int result = [self compare: f];
if (result == -1)
return YES;
else
return NO;
}

-(BOOL) isLessThanOrEqualTo:(Fraction *)f
{
int result = [self compare: f];
if (result == 0 || result == -1)
return YES;
else
return NO;
}

-(BOOL) isGreaterThan:(Fraction *)f
{
int result = [self compare: f];
if (result == 1)
return YES;
else
return NO;
}

-(BOOL) isGreaterThanOrEqualTo:(Fraction *)f
{
int result = [self compare: f];
if (result == 0 || result == 1)
return YES;
else
return NO;
}
@end

main.m

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];

[a setTo: 5 over: 1];
[b setTo: 6 over: 1];

// exercise 11.3
printf("a = %i/%i, b = %i/%i\n", a.numerator, a.denominator, b.numerator, b.denominator);
NSLog(@"Is a == b?: %@", [a isEqualTo: b]?@"YES":@"NO");
NSLog(@"Is a != b?: %@", [a isNotEqualTo: b]?@"YES":@"NO");
NSLog(@"Is a <  b?: %@", [a isLessThan: b]?@"YES":@"NO");
NSLog(@"Is a <= b?: %@",   [a isLessThanOrEqualTo: b]?@"YES":@"NO");
NSLog(@"Is a >  b?: %@", [a isGreaterThan: b]?@"YES":@"NO");
NSLog(@"Is a >= b?: %@",    [a isGreaterThanOrEqualTo: b]?@"YES":@"NO");

    [pool drain];
    return 0;
}
Last Edit: January 17, 2012, 07:35:55 PM by tknp Logged
alexander_TN
Newbie
*
Posts: 2


Email




Reply #1 on: February 09, 2012, 03:26:56 PM

Same issue here  Angry
Logged
alexander_TN
Newbie
*
Posts: 2


Email




Reply #2 on: February 09, 2012, 03:37:18 PM

The exercise asks for **informal** not formal protocol. So, the angled brackets are giving the error, I believe.
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #3 on: February 26, 2012, 10:46:11 AM

From what I can understand so far, these methods are simply inherited from the "category" extension of NSObject and there is no need to declare the informal protocol name itself in anyway?
Logged
elkie13
Newbie
*
Posts: 17






Reply #4 on: March 08, 2012, 10:00:05 AM

I am also not sure what he meant by this question.  I did it the same way as you. I wish this chapter had had a few examples of how to implement this stuff.
Logged
jacqueline
Newbie
*
Posts: 7






Reply #5 on: March 12, 2012, 02:03:59 AM

Hi All,

This is my solution.  I know it's not neat and compact, but for me, I like to spell it out.

NSComparisonMethods.h

#import <Foundation/Foundation.h>
#import "Fraction.h"

@interface Fraction (NSComparisonMethods)

@end

NSComparison.m

#import "NSComparisonMethods.h"

@implementation Fraction (NSComparisonMethods)

-(BOOL) isEqualTo: (id) object 

    if ( (double) numerator/denominator == (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 


-(BOOL) isLessThanOrEqualTo: (id) object 

    if ( (double) numerator/denominator <= (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 
}

-(BOOL) isLessThan: (id) object 

    if ( (double) numerator/denominator < (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 


-(BOOL) isGreaterThanOrEqualTo: (id) object 

    if ( (double) numerator/denominator >= (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 


-(BOOL) isGreaterThan: (id) object 

    if ( (double) numerator/denominator > (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 

-(BOOL) isNotEqualTo: (id) object 

    if ( (double) numerator/denominator != (double) [object numerator] / [object denominator] ) 
        return YES; 
    else 
        return NO; 


@end
Logged
jacqueline
Newbie
*
Posts: 7






Reply #6 on: March 12, 2012, 02:12:56 AM

#import "Fraction.h" 
#import "NSComparisonMethods.h" 


int main (int argc, const char * argv[])
{

        @autoreleasepool {
           
           
            Fraction *a, *b;
            BOOL result;
           
            a = [[Fraction alloc] init]; 
            b = [[Fraction alloc] init];
           

            [a setTo: 1 over: 4]; 
            [b setTo: 2 over: 4];
           
            NSLog(@"a is %i/%i", a.numerator, a.denominator);
            NSLog(@"b is %i/%i", b.numerator, b.denominator);
           
            NSLog(@"-----------------------");
           
            result=[a isEqualTo:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"is Equal to");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT Equal to");
                [b print];           
            }
           
            NSLog(@"-----------------------");
           
            result=[a isLessThanOrEqualTo:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"isLessThanOrEqualTo");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT LessThanOrEqualTo");
                [b print];           
            }
           
            NSLog(@"-----------------------");
           
            result=[a isLessThan:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"isLessThan:");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT LessThan:");
                [b print];           
            }
           
            NSLog(@"-----------------------");
           
            result=[a isGreaterThanOrEqualTo:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"isGreaterThanOrEqualTo:");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT GreaterThanOrEqualTo:");
                [b print];           
            }
           
            NSLog(@"-----------------------");
           
            result=[a isGreaterThan:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"isGreaterThan:");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT GreaterThan:");
                [b print];           
            }
           
            NSLog(@"-----------------------");
           
            result=[a isNotEqualTo:b];
           
            if (result ==YES)
            {
                [a print];
                NSLog(@"isNotEqualTo:");
                [b print];
               
            }
            else
            {
                [a print];
                NSLog(@"is NOT NotEqualTo:");
                [b print];           
            }
           
           
                            return 0;
}
}

And the results
2012-03-12 16:59:38.452 Excercise 11_3[940:707] a is 1/4
2012-03-12 16:59:38.454 Excercise 11_3[940:707] b is 2/4
2012-03-12 16:59:38.456 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.457 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.458 Excercise 11_3[940:707] is NOT Equal to
2012-03-12 16:59:38.458 Excercise 11_3[940:707] 2/4
2012-03-12 16:59:38.459 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.459 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.459 Excercise 11_3[940:707] isLessThanOrEqualTo
2012-03-12 16:59:38.460 Excercise 11_3[940:707] 2/4
2012-03-12 16:59:38.460 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.460 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.461 Excercise 11_3[940:707] isLessThan:
2012-03-12 16:59:38.461 Excercise 11_3[940:707] 2/4
2012-03-12 16:59:38.462 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.462 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.462 Excercise 11_3[940:707] is NOT GreaterThanOrEqualTo:
2012-03-12 16:59:38.463 Excercise 11_3[940:707] 2/4
2012-03-12 16:59:38.463 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.464 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.464 Excercise 11_3[940:707] is NOT GreaterThan:
2012-03-12 16:59:38.465 Excercise 11_3[940:707] 2/4
2012-03-12 16:59:38.465 Excercise 11_3[940:707] -----------------------
2012-03-12 16:59:38.465 Excercise 11_3[940:707] 1/4
2012-03-12 16:59:38.466 Excercise 11_3[940:707] isNotEqualTo:
2012-03-12 16:59:38.466 Excercise 11_3[940:707] 2/4
Program ended with exit code: 0
Logged
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.