Amazon.com Widgets Ex 11-1. My answer
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 09:18:07 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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 11
| | | |-+  Ex 11-1. My answer
Pages: [1]   Go Down
Print
Author Topic: Ex 11-1. My answer  (Read 342 times)
Nick Harvey
Newbie
*
Posts: 19






« on: March 18, 2012, 11:03:56 PM »

Here is my answer to Ex 11-1. As I have been modifying the same Fraction class files as I progress through the book, I needed to move my static variable gAddCounter to the MathOps category along with the class method to retrieve the value. This is just in case other people are growing the same class files as they progress through the book and find this useful.

As always, I welcome any feedback on good programming standards and practice.

#import "Fraction.h"

@interface Fraction (MathOps)

+(int) gAddCount;

-(Fraction *) add: (Fraction *) inFraction;
-(Fraction *) subtract: (Fraction *) inFraction;
-(Fraction *) multiply: (Fraction *) inFraction;
-(Fraction *) divide: (Fraction *) inFraction;
-(Fraction *) invert;
@end

#import "Fraction+MathOps.h"

static int gAddCount;

@implementation Fraction (MathOps)

+(int) gAddCount{
  extern int gAddCount;
  return gAddCount;
}

-(Fraction *) add: (Fraction *) inFr
{
  extern int gAddCount;
  gAddCount++;
 
  Fraction *retFr = [[Fraction allocF] init];
 
  [retFr setNumerator:((numerator * [inFr denominator]) + (denominator * [inFr numerator]))];
  [retFr setDenominator:(denominator * [inFr denominator])];
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) subtract:(Fraction *)inFr
{
  Fraction *retFr = [[Fraction allocF] init];
 
  [retFr setNumerator:((numerator * [inFr denominator]) - (denominator * [inFr numerator]))];
  [retFr setDenominator:(denominator * [inFr denominator])];
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) multiply:(Fraction *)inFr
{
  Fraction *retFr = [[Fraction allocF] init];
 
  [retFr setNumerator:(numerator * [inFr numerator])];
  [retFr setDenominator:(denominator * [inFr denominator])];
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) divide:(Fraction *)inFr
{
  Fraction *retFr = [[Fraction allocF] init];
 
  [retFr setNumerator:(numerator * [inFr denominator])];
  [retFr setDenominator:(denominator * [inFr numerator])];
 
  [retFr reduce];
 
  return retFr;
}

-(Fraction *) invert
{
  Fraction *retFr = [[Fraction allocF] init];
 
  [retFr setTo:denominator over:numerator]; 
  [retFr reduce];
 
  return retFr;
}

@end
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.