// 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 |