Computer Science Canada

[Obj-C] Helpful Links

Author:  wtd [ Sun Feb 22, 2004 12:12 am ]
Post subject:  [Obj-C] Helpful Links

The Objective-C Foundation Class are a library of classes and protocols devoid of dependence on either Apple's Cocoa API, or the GNUStep libaries.

http://ofc.sunsite.dk/

The Objective-C FAQ is always a good reference.

ftp://rtfm.mit.edu/pub/faqs/computer-lang/Objective-C/faq

Author:  rizzix [ Sun Feb 22, 2004 8:21 am ]
Post subject: 

nice.

got any programs you created in obj-c that you can show us?

Author:  wtd [ Mon Feb 23, 2004 12:42 am ]
Post subject: 

Unfortunately no. My recent work has been in C++ and Ruby primarily.

Author:  wtd [ Tue Feb 24, 2004 7:15 pm ]
Post subject: 

Saw a thread about a Fraction class in another forum here and thought it might make an educational example.

code:
// import the base class
#import <objc/Object.h>



@interface IntPair : Object
{
        @private
        int first, second;
}
- initWithFirst: (int) f andSecond: (int) s;
- (int) greater;
- (int) lesser;
- (bool) bothDivisibleBy: (int) t;
- (int) gcd;
- (int) first;
- (int) second;
- (void) first: (int) newValue;
- (void) second: (int) newValue;
- (void) swap;
@end



@interface Fraction : IntPair
- initWithNumerator: (int) initNum andDenominator: (int) initDenom;
- clone;
- (void) invert;
- (void) simplify;
- (int) numerator;
- (int) denominator;
- (void) numerator: (int) newValue;
- (void) denominator: (int) newValue;
- (void) simplifySigns;
- (void) multiplyBothBy: (int) multiplier;
- (int) wholeNumberValue;
- (Fraction *) remainder;
- (Fraction *) multiplyByFraction: (Fraction *) other;
- (Fraction *) multiplyByInt: (int) other;
@end




int main(int argc, char * argv[])
{
        Fraction * f = [[Fraction alloc] initWithNumerator: 2 andDenominator: 3];
        Fraction * s = [[Fraction alloc] initWithNumerator: 3 andDenominator: 4];
        Fraction * r = [f multiplyByFraction: s];
}




@implementation IntPair
- initWithFirst: (int) f andSecond: (int) s
{
        [super init];
        [self first: f];
        [self second: s];
        return self;
}

- (int) greater
{
        if ([self first] >= [self second])
                return [self first];
        else
                return [self second];
}

- (int) lesser
{
        if ([self first] <= [self second])
                return [self first];
        else
                return [self second];
}

- (bool) bothDivisibleBy: (int) t
{
        return ([self first] / t == 0 && [self second] / t == 0);
}

- (int) gcd
{
        int counter;
        for (counter = [self lesser]; counter <
}

- (int) first 
{
        return first; 
}

- (int) second
{
        return second;
}

- (void) first:  (int) newValue
{
        first = newValue;
}

- (void) second: (int) newValue
{
        second = newValue;
}

- (void) swap
{
        int temp = [self first];
        [self first: [self second]];
        [self second: temp];
}
@end



@implementation Fraction
- initWithNumerator: (int) initNum andDenominator: (int) initDenom
{
        [super initWithFirst: initNum andSecond: initDenom];
        [self denominator: initDenom;
        num   = initNum;
        return self;
}

- clone
{
        return [[Fraction alloc] initWithNumerator: num andDenominator: denom];
}

- (void) invert
{
        [self swap];
}

- (void) simplify
{
       
}

- (int) numerator {
        return [self first];
}

- (int) denominator
{
        return [self second];
}

- (void) numerator: (int) newValue
{
        [self first: newValue];
}

- (void) denominator: (int) newValue
{
        [self second: newValue];
        [self simplifySigns];
}

- (void) simplifySigns
{
        if ([self denominator] < 0)
                [self multiplyBothBy: -1];
}

- (void) multiplyBothBy: (int) multiplier
{
        [self first:  [self first]  * multiplier];
        [self second: [self second] * multiplier];
}

- (int) wholeNumberValue
{
        return [self numerator] / [self denominator];
}

- (Fraction *) remainder
{
        return [[Fraction alloc]
                initWithNumerator: [self numerator] % [self denominator]
                andDenominator:    [self denominator]];
}

- (Fraction *) multiplyByFraction: (Fraction *) other
{
        return [[Fraction alloc]
                initWithNumerator: [self numerator]   * [other numerator]
                andDenominator:    [self denominator] * [other denominator]];
}

- (Fraction *) multiplyByInt: (int) other
{
        return [[Fraction alloc]
                initWithNumerator: [self numerator] * other
                andDenominator:    [self denominator]];
}
@end

Author:  rizzix [ Tue Feb 24, 2004 11:17 pm ]
Post subject: 

nice.. thats all that i needed a simple example.

Author:  wtd [ Tue Feb 24, 2004 11:23 pm ]
Post subject: 

You're welcome.

I hope all of the syntax there was easy to follow. I didn't get mean and use Protocols, just because it wasn't warranted, though. Wink


: