I thought this was quite difficult, but not sure if I did what I was supposed to do.

Anyway, here is my solution.

Your (kind) feedback would be appreciated.

Comparison.h

Code:

//
// Comparison.h
// Exercise_2.0_11_2
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Fraction.h"
@interface Fraction (Comparison)
/*-(BOOL) isEqualTo : (Fraction *) f; */
-(int) compare: (Fraction *) f;
@end

Comparison.m

Code:

//
// Comparison.m
// Exercise_2.0_11_2
//
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Comparison.h"
@implementation Fraction (Comparison)
/*-(BOOL) isEqualTo : (id) f
{
Fraction *a = [ [ Fraction alloc] initWith: numerator over: denominator];
Fraction *b = [ [ Fraction alloc] initWith: [f numerator] over: [f denominator] ];
BOOL sameNess = NO;
[a reduce];
[b reduce];
if ( a.numerator == b.numerator && a.denominator == b.denominator)
sameNess = YES;
[a release];
[b release];
return sameNess;
}
*/
-(int) compare: (Fraction *) f
{
Fraction *a = [ [ Fraction alloc] initWith: numerator over: denominator];
double f1, f2;
f1= [a convertToNum];
f2 = [ f convertToNum];
[a release];
if ( f1 < f2)
return -1;
else if (f1 > f2)
return 1;
else
return 0;
}
@end

Fraction.h

Code:

//
// Fraction.h
// Chap 7 Prgm 7.1
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Fraction : NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
/* implement informal protocol */
- (BOOL)isEqualTo:(id)object;
- (BOOL)isLessThanOrEqualTo:(id)object;
- (BOOL)isLessThan:(id)object;
- (BOOL)isGreaterThanOrEqualTo:(id)object;
- (BOOL)isGreaterThan:(id)object;
- (BOOL)isNotEqualTo:(id)object;
+(Fraction *) allocF;
+ (int) count;
+ (int) gAddMethodUsageCounter;
static int gCount;
static int gAddMethodUsageCounter;
/** class initialization **/
- initWith: (int) n over: (int) d;
-(void) reduce;
/* modified accessor*/
-(void) setTo: (int) n over: (int) d;
/*utilities*/
-(double) convertToNum;
/* basic utility */
/*********SPECIFIC TO EXERCISE 7-5 ***************/
-(void) print;
/*********SPECIFIC TO EXERCISE 7-5 ***************/
-(void) printReduced: (BOOL) reduce;
@end

Fraction.m

Code:

//
// Fraction.m
// Chap 7 Prgm 7.1
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Fraction.h"
#import "Comparison.h"
@implementation Fraction;
@synthesize denominator, numerator;
+ (int) gAddMethodUsageCounter
{
return gAddMethodUsageCounter;
}
+(Fraction *) allocF{
extern int gCount;
++gCount;
return [Fraction alloc];
}
+ (int) count
{
extern int gCount;
return gCount;
}
- (BOOL)isEqualTo:(id) f
{
if ( ! self)
return NO;
Fraction *a = [ [ Fraction alloc] initWith: numerator over: denominator];
Fraction *b = [ [ Fraction alloc] initWith: [f numerator] over: [f denominator] ];
BOOL sameNess = NO;
[a reduce];
[b reduce];
if ( a.numerator == b.numerator && a.denominator == b.denominator)
sameNess = YES;
[a release];
[b release];
return sameNess;
}
// Implemented using isEqual:. Returns NO if receiver is nil.
- (BOOL)isLessThanOrEqualTo:(id) f
{
if ( !self)
return NO;
if ( [ self compare: f] == -1 || [ self isEqualTo: f])
return YES;
else
return NO;
}
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isLessThan:(id) f
{
if ( !self)
return NO;
if ( [ self compare: f] == -1 )
return YES;
else
return NO;
}
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isGreaterThanOrEqualTo:(id) f
{
if ( !self)
return NO;
if ( [ self compare: f] == 1 || [ self isEqualTo: f] )
return YES;
else
return NO;
}
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isGreaterThan:(id) f
{
if ( !self)
return NO;
if ( [ self compare: f] == 1 )
return YES;
else
return NO;
}
// Implemented using compare. Returns NO if receiver is nil.
- (BOOL)isNotEqualTo:(id) f
{
if ( !self)
return NO;
if ( ! [ self isEqualTo: f] )
return YES;
else
return NO;
}
// Implemented using compare. Returns NO if receiver is nil.
-(void) print
{
NSLog(@"%i/%i ", numerator, denominator);
}
- initWith: (int) n over: (int) d
{
if ( ! ( self = [super init]))
return nil;
if (self)
[self setTo: n over: d];
return self;
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
/*********SPECIFIC TO EXERCISE 7-5 ***************
-(void) print
{
float f = 0.00;
if ( (f = (float) numerator / denominator) > 1)
NSLog(@"%i %i/%i", numerator/denominator, numerator % denominator, denominator);
else
NSLog(@"%i/%i ", numerator, denominator);
}
*********SPECIFIC TO EXERCISE 7-5 ***************/
-(void) printReduced: (BOOL) reduce
{
if (reduce == YES){
Fraction *myF = [ [ Fraction alloc] init];
myF.numerator = numerator;
myF.denominator = denominator;
[myF reduce];
NSLog(@"%i/%i", myF.numerator, myF.denominator);
[myF release];
}
else
NSLog(@"%i/%i ", numerator, denominator);
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
/*
-(Fraction *) add: (Fraction *) f
{
int resultNum, resultDenom;
Fraction* result = [[Fraction alloc] init];
resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return result;
}
*/
-(void) reduce
{
int u, v, temp;
BOOL isNegativeNumerator = NO;
BOOL isNegativeDenominator = NO;
if ( numerator < 0)
{
isNegativeNumerator = YES;
numerator = -numerator;
}
if ( denominator < 0)
{
isNegativeDenominator = YES;
denominator = -denominator;
}
u = numerator;
v = denominator;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
if ( isNegativeNumerator == YES || isNegativeDenominator == YES)
numerator = -numerator;
}
@end

main.h

Code:

//
// Prefix header for all source files of the 'Exercise_2.0_11_2' target in the 'Exercise_2.0_11_2' project.
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif

main.m

Code:

/*#import "MathOps.h"*/
#import "Fraction.h"
#import "Comparison.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *a = [[Fraction alloc] initWith: 7 over: 8];
Fraction *b = [ [ Fraction alloc] initWith: 14 over: 16];
Fraction *c = [ [ Fraction alloc] initWith: 3 over: 4];
if ( [ a isEqualTo: b] == YES)
{
[a print];
NSLog(@"is Equal to");
[b print];
}
NSLog(@"\n");
if ( [ a isLessThanOrEqualTo: b] == YES)
{
[a print];
NSLog(@"is Less than or Equal to");
[b print];
}
NSLog(@"\n");
if ( [ c isLessThanOrEqualTo: a] == YES)
{
[c print];
NSLog(@"is Less than or Equal to");
[a print];
}
NSLog(@"\n");
if ( [ c isGreaterThanOrEqualTo: a] == YES)
{
[c print];
NSLog(@"is greater than or Equal to");
[a print];
}
NSLog(@"\n");
if ( [ c isLessThan: a] == YES)
{
[c print];
NSLog(@"is Less than");
[a print];
}
NSLog(@"\n");
if ( [ a isGreaterThanOrEqualTo: c] == YES)
{
[a print];
NSLog(@"is greater than or Equal to");
[c print];
}
NSLog(@"\n");
if ( [ a isGreaterThan: c] == YES)
{
[a print];
NSLog(@"is greater than");
[c print];
}
NSLog(@"\n");
if ( [ a isNotEqualTo: c] == YES)
{
[a print];
NSLog(@"is not equal to");
[c print];
}
[a release];
[b release];
[c release];
[pool drain];
return 0;
}