Welcome, Guest. Please login or register.
Did you miss your activation email?
April 25, 2017, 03:26:49 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
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 9
| | |-+ Chapter 9 - Exercise 4 Solution
Pages: [1] Go Down
Print
Author Topic: Chapter 9 - Exercise 4 Solution (Read 8781 times)
bertobettencourt
Newbie
*
Posts: 45


Email
on: October 23, 2013, 10:49:52 AM

Complex.h

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Complex : NSObject
@property double real, imaginary;


-(void) setReal:(double) a andImaginary:(double) b;
-(void) print; // Displays a + bi
-(id) addmeC: (id)f; //pretty straight foward, instead of returning class (name *) data type, it returns objects (id).


@end


Complex.m

Code: (Objective-C)
#import "Complex.h"

@implementation Complex
@synthesize real, imaginary;


-(void) setReal:(double) a andImaginary:(double) b{
    real = a;
    imaginary = b;
}

-(void) print{
    NSLog(@"Complex Print: %g + %gi", real, imaginary);
}

-(id) addmeC: (id)f{
   
    id result = [[Complex alloc]init]; // allocate an object with id and name it, in this case, result.
   
    [result setReal: real + [f real]]; // remember id does not use the DOT OPERATOR (result.numerator <- WRONG), you must use the bracket instance call [object method], and you must use the setters for id to work.
    [result setImaginary: imaginary + [f imaginary]]; //same as above
   
    return result;
}

@end


Fraction.h

Code: (Objective-C)
#import <Foundation/Foundation.h>

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

-(id) addmeF: (id) f;
-(void) print;
-(void) setTo: (double) r over: (double) i;
-(void) reduce;


@end


Fraction.m

Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

-(id) addmeF: (id) f{
   
    id sum = [[Fraction alloc]init];
    [sum setNumerator: numerator + [f numerator]];
    [sum setDenominator: denominator * [f denominator]];
   
    return sum;
}

-(void) print{
   
    NSLog(@"Fraction Print: %.1f . %.1f", numerator, denominator);
   
}

-(void) setTo: (double) r over: (double) i{
   
    numerator = r;
    denominator = i;
   
}

-(void) reduce{
   
    int u = numerator;
    int v = denominator;
    int temp;

    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator/= u;
}

@end


main.m

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Complex.h"
#import "Fraction.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        id dataValue1, dataValue2, result; // declare your ids.
       
        Fraction *f1 = [[Fraction alloc]init];
        Fraction *f2 = [[Fraction alloc]init];
       
        Complex *c1 = [[Complex alloc]init];
        Complex *c2 = [[Complex alloc]init];

       
        [f1 setTo:2 over:5];
        [f2 setTo:5 over:2];
       
        [c1 setReal:5 andImaginary:10];
        [c2 setReal:16 andImaginary:35];
       
        dataValue1 = f1; // assign data1 to f1
        dataValue2 = f2; // assign data2 to f2
       
        result = [dataValue1 addmeF:dataValue2]; // result = f1 add f2.
        [result print]; // print method.
       
        dataValue1 = c1;
        dataValue2 = c2;
       
        result = [dataValue1 addmeC:dataValue2];
        [result print];
   
                    }
   
    return 0;
}
Logged
Coder_1696
Newbie
*
Posts: 5


Reply #1 on: May 24, 2015, 04:16:06 AM

Quote
ANSWER TO EXCERCISE 4:
Code: (Objective-C)
@interface FractionTests : NSObject
@property int numerator, denominator;
-(void) print: (BOOL) toReduceOrNotReduce;
-(int) setTo: (int) n over: (int) d;
-(void) reduce;
-(id) addmeF:(id)f;

@end

#import "FractionTests.h"

@implementation FractionTests
@synthesize numerator, denominator;
-(int) setTo:(int) n over: (int) d {
    numerator = n;
    denominator = d;
    return numerator;
    return denominator;
}

-(void) reduce {
    int u = numerator;
    int v = denominator;
    int temp;
   
    while (v != 0) {
        temp = u % v; //GCD for fraction then dividing below.
        //NSLog(@"%i",temp);
        u = v;
        //NSLog(@"%i", u);
        v = temp;
        //NSLog(@"%i", v);
       
     
    }
    numerator /= u;
    denominator /= u;
    //NSLog(@"Reduced Value equals %i/%i", numerator, denominator);
}
-(id) addmeF:(id)f {
    id Fraction = [FractionTests new];
    FractionTests *result = [[FractionTests alloc] init];
    result.numerator =  numerator * [f denominator] + denominator * [f numerator];
    result.denominator = denominator * [f denominator];
   
    [result reduce];
    Fraction = result;
    return Fraction;
}

@interface Complex : NSObject
@property int real, imaginary;
-(void) print;
-(void) setTo: (int)R with: (int) i;
-(id) addmeC:(id)c;

@end

@implementation Complex
@synthesize real, imaginary;
-(void) setTo: (int)R with:(int) i {
    real = R;
    imaginary = i;
}

-(id) addmeC:(id)c; {
    id C;
    Complex *New = [[Complex alloc] init];
    New.real = real + [c real];
    New.imaginary = imaginary + [c imaginary];
   
    C = New;
    return C;
}

-(void) print {
    NSLog(@"%i + %ii", real, imaginary);
}

@end

#import <Foundation/Foundation.h>
#import "FractionTests.h"
#import "Complex.h"
#import "XYPoint.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {

        FractionTests *afraction = [[FractionTests alloc] init];
        FractionTests *bfraction = [[FractionTests alloc] init];
        Complex *RI = [Complex new];
        Complex *RI2 = [Complex new];
        FractionTests *resultFraction;
       // Complex *neww;
       
        id dataValue1, dataValue2, result;
        id XY;
        XY = [XYPoint new];
        [XY setX:7 andY:56];
        [XY print];
       
        [afraction setTo:1 over:4];
        [bfraction setTo:2 over:6];
        dataValue1 = afraction;
        dataValue2 = bfraction;
       
        result = [dataValue1 addmeF:dataValue2];
        [result print:YES];
       
        [RI setTo:10 with:2];
        [RI2 setTo:34 with:20];
        dataValue1 = RI;
        dataValue2 = RI2;
       
        result = [dataValue1 addmeC:dataValue2];
        [result print];
       
Last Edit: May 24, 2015, 04:17:42 AM by Coder_1696 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.