Welcome, Guest. Please login or register.
Did you miss your activation email?
July 23, 2016, 07:58:34 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 7
| | | |-+ Chapter 7 exercise 2
Pages: [1] Go Down
Author Topic: Chapter 7 exercise 2 (Read 1006 times)
Posts: 17

on: February 22, 2012, 04:29:26 PM

Here's what I think you were after in Ex 2.  You wanted us to be able to tell the print method if the number needed reducing?  So I added the BOOL argument to the print method.  Then I removed the reduce line from the add method.  In the main I added the two fractions and called print method first with requesting reduction then without to prove I did not permanently change the result.
------------in fraction.m---------------------
-(void) print:(BOOL)r;
    Fraction *display = [[Fraction alloc]init];
    [display setTo:numerator over:denominator];
    if (r)
        [display reduce];
-(Fraction *) add:(Fraction *)f
    Fraction *result = [[Fraction alloc]init];
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
    return result;
--------------------in main.m
int main (int argc, const char * argv[])

    @autoreleasepool {
        Fraction *aFraction =[[Fraction alloc]init];
        Fraction *bFraction =[[Fraction alloc]init];   
        Fraction *resultFraction;
        [aFraction setTo:1 over:2];
        [bFraction setTo:1 over:4];
        resultFraction = [aFraction add:bFraction];
        [resultFraction print:YES];
        [resultFraction print:NO];
    return 0;
Pages: [1] Go Up
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.