Amazon.com Widgets Chapter 10 Exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 08:20:08 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
| |-+  Exercises
| | |-+  Chapter 10
| | | |-+  Chapter 10 Exercise 5
Pages: [1]   Go Down
Print
Author Topic: Chapter 10 Exercise 5  (Read 570 times)
clouded
Full Member
***
Posts: 123






« on: May 31, 2012, 01:08:42 PM »

Here's my version of this assignment:

Code: (Objective-C)
// Chapter 10 Exercise 5
// Using typedef, define a type called FractionObj that enables you to write statements
// such as the following:
//    FractionObj f1 = [[Fraction alloc] init],
//    f2 = [[Fraction alloc] init];

//  main.m

#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, char * argv[])
{
    @autoreleasepool {
        
        typedef Fraction *FractionObj;
        
        FractionObj f1 = [[Fraction alloc] init], f2 = [[Fraction alloc] init];
        
        [f1 setTo: 1 over: 2];
        [f2 setTo: 3 over: 4];
        
        [f1 print];
        NSLog(@" ");
        [f2 print];
        
    }
    return 0;
}
Code: (Objective-C)
//  Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject
@property int numerator, denominator;

-(void) print;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) negate;
-(void) reduce;
-(id) addN: (id) f;
+(Fraction *) allocF;
+(int) count;
+(int) addCount;
@end
Code: (Objective-C)
// Fraction.m

#import "Fraction.h"

static int gCounter;
static int gAddCounter;

@implementation Fraction
@synthesize numerator, denominator;

+(Fraction *) allocF
{
    extern int gCounter;
    ++gCounter;
    return [Fraction alloc];
}
+(int) count
{
    extern int gCounter;
    return gCounter;
}
+(int) addCount
{
    extern int gAddCounter;
    return gAddCounter;
}
-(void) print
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator;
    result.denominator = denominator;
    
    if (numerator/denominator >= 1 || numerator/denominator <= -1) {
        if (result.numerator % result.denominator == 0) {
            NSLog(@"%i \nOR", result.numerator / result.denominator);
        } else if (result.numerator <= 0) {
            NSLog(@"%i %i/%i \nOR", result.numerator / result.denominator, -(result.numerator % result.denominator) , result.denominator);
        } else {
            NSLog(@"%i %i/%i \nOR", result.numerator / result.denominator, result.numerator % result.denominator , result.denominator);
        }
    }
    [result reduce];
    NSLog (@"%i/%i", result.numerator, result.denominator);
}
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
-(void) setTo: (int) n over: (int) d
{
    numerator = n;
    denominator = d;
    if (d < 0) {
        [self negate];
    }
}
-(void) negate
{
    denominator *= -1;
    numerator *= -1;
}
-(Fraction *) add: (Fraction *) f
{
    extern int gAddCounter;
    gAddCounter++;
    
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    [result reduce];
    return result;
}
-(Fraction *) subtract: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator - denominator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    [result reduce];
    return result;
}
-(Fraction *) multiply: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    [result reduce];
    return result;
}
-(Fraction *) divide: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator;
    result.denominator = denominator * f.numerator;
    
    [result reduce];
    return result;
}
-(void) reduce
{
    int u = numerator;
    int v = denominator;
    int temp;
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator /= u;
    if (denominator < 0) {
        [self negate];
    }
}
-(id) addN: (id) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * [f denominator] + denominator * [f numerator];
    result.denominator = denominator * [f denominator];
    
    [result reduce];
    return result;
}
@end

Output:

1/2

3/4
Logged
Gordio
Newbie
*
Posts: 12


Email




« Reply #1 on: August 05, 2012, 12:55:49 PM »

I just wanted to say I appreciate you answering almost all the questions of every chapter!  I wish the author provided the solutions, instead of relying on the readers to come up with the solutions and discuss.

They're very helpful for me, who is a self-taught programmer.  Sometimes I get a solution, but it's not as clean as it could be, and your solutions (which seem like they're the best written of any member here) are extremely helpful.
Logged
clouded
Full Member
***
Posts: 123






« Reply #2 on: August 11, 2012, 08:04:09 AM »

Glad to help!   Smiley
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #3 on: August 21, 2012, 06:32:39 AM »

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        typedef Fraction *FractionObj;
       
        FractionObj f1 = [[Fraction alloc] init],
        f2 = [[Fraction alloc] init];
       
        [f1 setTo:1 over:2];
        [f2 setTo:2 over:3];
       
        [f1 print];
        [f2 print];
       
    }
    return 0;
}

2012-08-21 14:31:12.444 FractionTest[472:303] 1/2
2012-08-21 14:31:12.446 FractionTest[472:303] 2/3
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.