Welcome, Guest. Please login or register.
Did you miss your activation email?
July 26, 2016, 12:53:59 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
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 11
| | |-+ Exercise 3
Pages: [1] Go Down
Print
Author Topic: Exercise 3 (Read 896 times)
kotyara
Newbie
*
Posts: 15


on: December 14, 2014, 12:00:53 PM

I had to put Fraction instead of NSObject in declaration and implementation parts of the program to move on with it. Without this change error messages popped up.

//  Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) reduce;
@end

@interface Fraction (NSComparisonMethods)
-(BOOL) isEqualTo:(id)object;
-(BOOL) isLessThanOrEqualTo:(id)object;
-(BOOL) isLessThan:(id)object;
-(BOOL) isGreaterThanOrEqualTo:(id)object;
-(BOOL) isGreaterThan:(id)object;
-(BOOL) isNotEqualTo:(id)object;
@end

//  Fraction.m

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;
...
method implementations
...
@end

@implementation Fraction (NSComparisonMethods)
-(BOOL)isEqualTo:(id)object{
    if (self.denominator != 0 && [object denominator]!= 0) {
        double a, b;
        a = (double)self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a == b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
-(BOOL) isLessThanOrEqualTo:(id)object{
    if (self.denominator != 0 && [object denominator] != 0) {
        double a,b;
        a = (double) self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a <= b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
-(BOOL) isLessThan:(id)object{
    if (self.denominator != 0 && [object denominator] != 0) {
        double a,b;
        a = (double) self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a < b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
-(BOOL) isGreaterThanOrEqualTo:(id)object{
    if (self.denominator != 0 && [object denominator] != 0) {
        double a,b;
        a = (double) self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a >= b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
-(BOOL) isGreaterThan:(id)object{
    if (self.denominator != 0 && [object denominator] != 0) {
        double a,b;
        a = (double) self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a > b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
-(BOOL)isNotEqualTo:(id)object{
    if (self.denominator != 0 && [object denominator]!= 0) {
        double a, b;
        a = (double)self.numerator / self.denominator;
        b = (double) [object numerator] / [object denominator];
        return a != b;
    } else {
        NSLog(@"Error - one (or both) of denominators is zero");
        return NAN;
    }
}
@end
Logged
cary_W
Newbie
*
Posts: 10


Email
Reply #1 on: June 13, 2016, 05:07:52 PM

I think instead of doing this two line all the time, I think you can invoke the compare: method to get the difference, and if you like, you can use the conditional operator.
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.