Amazon.com Widgets Exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 23, 2014, 10:49:59 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 7
| | | |-+ Exercise 5
Pages: [1] Go Down
Print
Author Topic: Exercise 5 (Read 638 times)
MCaEd
Newbie
*
Posts: 49






on: October 10, 2011, 09:33:32 AM

Here is my code for print method:

Code: (Objective-C)
-(void) print
{
    if (numerator > denominator)
    {
        int entire = numerator / denominator;
        int newNum = numerator % denominator;
        Fraction *newFrac = [[Fraction alloc] init];
       
        newFrac.numerator = newNum;
        newFrac.denominator = denominator;
       
        [newFrac reduce];
       
        if (!newFrac.numerator)
            NSLog (@"%i", entire);
        else
            NSLog (@"%i %i/%i", entire, newFrac.numerator, newFrac.denominator);
       
        [newFrac release];
    }
    else if (numerator == denominator)
    {
        int oneNumber = numerator / denominator;
        NSLog (@"%i (whole)", oneNumber);
    }
    else if (!denominator)
        NSLog (@"NAN");
}

And I have print: method included from the previous exercises as well:

Code: (Objective-C)
-(void) print: (BOOL) lessen
{
    if (lessen)
    {
        Fraction *testFrac = [[Fraction alloc] init];
        [testFrac setTo: numerator over: denominator];
        [testFrac reduce];
       
        if (testFrac.denominator < 0)
        {
            testFrac.denominator = -testFrac.denominator;
            testFrac.numerator = -testFrac.numerator;
        }
       
        NSLog (@"%i/%i (reduced)", testFrac.numerator, testFrac.denominator);
        [testFrac release];
    }
    else
    {
        if (denominator < 0)
        {
            numerator = -numerator;
            denominator = -denominator;
        }
        NSLog (@"%i/%i (non-reduced)", numerator, denominator);
    }
}

If it's possible to reduce the code or make it better, feel free to leave a comment/message.
Thanks
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.