Amazon.com Widgets Chapter 7 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 04:06:04 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
Print
Author Topic: Chapter 7 Exercise 2 (Read 6073 times)
jacqueline
Newbie
*
Posts: 7






on: March 01, 2012, 06:21:50 PM

The question is "Modify the print method for your Fraction class so that it takes an additional BOOL argument that indicates whether the fraction should be reduced for display.  If it is to be reduced (that is if the argument is YES), be sure not to make any permanent changes to the fraction itself".

Okay, so I altered the print method, implemented the BOOL argument AND included the "shouldReduce" to make it one method.

- (void) print
{
    BOOL shouldReduce;
       
   
    shouldReduce = YES;
   
    if (shouldReduce == YES)
       
    {
       
        int u = numerator;
        int v = denominator;
        int temp;
       
        while (v!= 0) {
            temp = u % v;
            u = v;
            v = temp;
        }
       
        numerator /= u;
        denominator /= u;   
    }
   
    NSLog(@"%i/%i", numerator, denominator);

}


When BOOL is YES, these are the results:

2012-03-02 09:12:03.518 Excercise 7_2[641:707] 1/2
2012-03-02 09:12:03.520 Excercise 7_2[641:707] +
2012-03-02 09:12:03.520 Excercise 7_2[641:707] 1/6
2012-03-02 09:12:03.521 Excercise 7_2[641:707] =
2012-03-02 09:12:03.521 Excercise 7_2[641:707] 2/3
2012-03-02 09:12:03.521 Excercise 7_2[641:707]  -----------------------------
2012-03-02 09:12:03.522 Excercise 7_2[641:707] 1/2
2012-03-02 09:12:03.522 Excercise 7_2[641:707] -
2012-03-02 09:12:03.523 Excercise 7_2[641:707] 1/6
2012-03-02 09:12:03.523 Excercise 7_2[641:707] =
2012-03-02 09:12:03.523 Excercise 7_2[641:707] 1/3
2012-03-02 09:12:03.524 Excercise 7_2[641:707]  -----------------------------
2012-03-02 09:12:03.524 Excercise 7_2[641:707] 1/2
2012-03-02 09:12:03.525 Excercise 7_2[641:707] *
2012-03-02 09:12:03.525 Excercise 7_2[641:707] 1/6
2012-03-02 09:12:03.525 Excercise 7_2[641:707] =
2012-03-02 09:12:03.526 Excercise 7_2[641:707] 1/12
2012-03-02 09:12:03.526 Excercise 7_2[641:707]  -----------------------------
2012-03-02 09:12:03.526 Excercise 7_2[641:707] 1/2
2012-03-02 09:12:03.527 Excercise 7_2[641:707] /
2012-03-02 09:12:03.527 Excercise 7_2[641:707] 1/6
2012-03-02 09:12:03.527 Excercise 7_2[641:707] =
2012-03-02 09:12:03.528 Excercise 7_2[641:707] 3/1
Program ended with exit code: 0

When BOOL is NO, here are the results:
2012-03-02 09:19:33.697 Excercise 7_2[707:707] 1/2
2012-03-02 09:19:33.699 Excercise 7_2[707:707] +
2012-03-02 09:19:33.699 Excercise 7_2[707:707] 1/6
2012-03-02 09:19:33.700 Excercise 7_2[707:707] =
2012-03-02 09:19:33.700 Excercise 7_2[707:707] 8/12
2012-03-02 09:19:33.700 Excercise 7_2[707:707]  -----------------------------
2012-03-02 09:19:33.701 Excercise 7_2[707:707] 1/2
2012-03-02 09:19:33.701 Excercise 7_2[707:707] -
2012-03-02 09:19:33.702 Excercise 7_2[707:707] 1/6
2012-03-02 09:19:33.702 Excercise 7_2[707:707] =
2012-03-02 09:19:33.703 Excercise 7_2[707:707] 4/12
2012-03-02 09:19:33.708 Excercise 7_2[707:707]  -----------------------------
2012-03-02 09:19:33.714 Excercise 7_2[707:707] 1/2
2012-03-02 09:19:33.715 Excercise 7_2[707:707] *
2012-03-02 09:19:33.718 Excercise 7_2[707:707] 1/6
2012-03-02 09:19:33.719 Excercise 7_2[707:707] =
2012-03-02 09:19:33.719 Excercise 7_2[707:707] 1/12
2012-03-02 09:19:33.720 Excercise 7_2[707:707]  -----------------------------
2012-03-02 09:19:33.721 Excercise 7_2[707:707] 1/2
2012-03-02 09:19:33.721 Excercise 7_2[707:707] /
2012-03-02 09:19:33.722 Excercise 7_2[707:707] 1/6
2012-03-02 09:19:33.722 Excercise 7_2[707:707] =
2012-03-02 09:19:33.723 Excercise 7_2[707:707] 6/2
Program ended with exit code: 0

If this isn't what was required or it's not right, please advise and be merciful to a woman who wishes she took up computer science instead of accounting Smiley

cheers
jacqueline
Logged
clouded
Full Member
***
Posts: 123






Reply #1 on: May 14, 2012, 03:35:55 PM

Here's what I think the exercise was asking for, not sure, but it's code what the heck!  Grin

Code: (Objective-C)
// Chapter 7 Exercise 2.
// Modify the print method from your Fraction class so that it takes an
// additional BOOL argument that indicates whether the fraction should
// be reduced for display. If it is to be reduced (that is if the argument is
// YES), be sure not to make any permanent changes to the fraction itself.

// main.m

#import "Fraction.h"
int main (int argc, char * argv[]) {
   
    @autoreleasepool {
       
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
       
        Fraction *resultFraction;
       
        [aFraction setTo: 2 over: 8]; // set 1st fraction to 1/4
        [bFraction setTo: 1 over: 2]; // set 2nd fraction to 1/2
       
        [aFraction print: 1];
        NSLog (@"+");
        [bFraction print: 0];
        NSLog (@"=");
       
        resultFraction = [aFraction add: bFraction];
        [resultFraction print: 0];
       
    }
    return 0;
}

// Fraction.h

#import <Foundation/Foundation.h>

// Define the Fraction class
@interface Fraction : NSObject
@property int numerator, denominator;

-(void) print: (bool) b;
-(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) reduce;
@end

// Fraction.m

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

-(void) print: (bool) b
{
    if (b == 1) {
        Fraction *result = [[Fraction alloc] init];
        result.numerator = numerator;
        result.denominator = denominator;
       
        [result reduce];
        NSLog (@"%i/%i", result.numerator, result.denominator);
    } else {
        NSLog (@"%i/%i", numerator, denominator);
    }
}
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
-(void) setTo: (int) n over: (int) d
{
    numerator = n;
    denominator = d;
}
-(Fraction *) add: (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 *) 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;
}
@end

Output:

1/4
+
1/2
=
3/4
Logged
stuartjj
Newbie
*
Posts: 11






Reply #2 on: June 08, 2012, 06:38:14 AM

I think that I have code similar to Clouded, but I have three errors:
1) Incomplete Implementation.

2) for the line:
Code: (Objective-C)
-(void) print (BOOL) reduceIfNecessary
I get the error: Expected method body, and
3) two lines down:
Code: (Objective-C)
if (reduceIfNecessary==1)
I get the error: Use of undeclared identifier 'reduceIfNecessary'.  

I'm not sure how I screwed up the boolean argument.  Any suggestions?  Thanks!
Code: (Objective-C)
#import <Foundation/Foundation.h>

// The Fraction class

@interface Fraction : NSObject

@property int numerator, denominator;

-(void)     setTo: (int) n over: (int) d;
-(double)   convertToNum;

-(Fraction *)   add:(Fraction *)f;
-(Fraction *) subtract:(Fraction *)f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;

-(void) flipTheUnaryMinusSignIfInDenominator;
-(void) print: (BOOL) reduceIfNecessary;
-(void) reduce;

@end

#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) print (BOOL) reduceIfNecessary
{


if (reduceIfNecessary==1)
{
    Fraction *display = [[Fraction alloc] init];
    [display setTo:numerator over:denominator];
    
    [display reduce];
    NSLog (@"%i/%i", display.numerator, display.denominator);
}
else
{
    [self flipTheUnaryMinusSignIfInDenominator];
    NSLog (@"%i/%i", numerator, denominator);
}


}


-(void) flipTheUnaryMinusSignIfInDenominator
{  
    if (denominator <0)
    {denominator=-1*denominator;
        numerator=-1*numerator;
    }
}

-(void) reduce
{  
    int u = numerator;  
    int v = denominator;  
    int temp;  
    while (v != 0) {  
        temp = u % v;  
        u = v;  
        v = temp;  
    }  
    numerator /= u;  
    denominator /= u;  
    
    [self flipTheUnaryMinusSignIfInDenominator];  
}
    
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
-(void) setTo:(int)n over:(int)d
{
    numerator =n;
    denominator=d;
}


-(Fraction *) add: (Fraction *) f
{
    //to add two fractions:
    // a/b + c/d = ((a*d) + (b*c)) / (b * d)
    
    //result will store the result of the addition
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
    
    [result flipTheUnaryMinusSignIfInDenominator];
    return result;
}

-(Fraction *) subtract:(Fraction *)f
{
    // a/b - c/d = ((a*d) - (b*c)) / (b * d)
    Fraction *result = [[Fraction alloc] init];

    result.numerator = (numerator * f.denominator) - (denominator*f.numerator);
    result.denominator = denominator * f.denominator;

    [result flipTheUnaryMinusSignIfInDenominator];
    return result;
}

-(Fraction *) multiply:(Fraction *)f
{
    //a/b * c/d = (a*c)  / (b*d)
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.numerator;
    result.denominator = denominator *f.denominator;
    
    [result flipTheUnaryMinusSignIfInDenominator];
    return result;
    
}

-(Fraction *) divide:(Fraction *)f
{
    //a/b   /  c/d  = a*d   /    b*c
    Fraction *result = [[Fraction alloc] init];
    
    result.numerator = numerator * f.denominator;
    result.denominator = denominator *f.numerator;
    
    [result flipTheUnaryMinusSignIfInDenominator];
    return result;
}
//main.m
#import "Fraction.h"

int main (int argc, char * argv[])
{
    @autoreleasepool {
        Fraction  *aFraction = [[Fraction alloc] init];
        Fraction  *bFraction = [[Fraction alloc] init];
        
        Fraction *resultFraction;
        
        // set fraction to 1/3
        
        [aFraction setTo: -1 over: 4];
        [bFraction setTo: -1 over: 2];
        
        
        // display the fraction
        
        [aFraction print: 0];
        NSLog (@"+");
        [bFraction print: 0];
        NSLog (@"=");
        
        resultFraction = [aFraction add: bFraction];
        [resultFraction print: 1];
        
        [aFraction print: 0];
        NSLog (@"-");
        [bFraction print: 0];
        NSLog (@"=");
        
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print: 1];
        
        [aFraction print: 0];
        NSLog (@"*");
        [bFraction print: 0];
        NSLog (@"=");
        
        resultFraction = [aFraction multiply:bFraction];
        [resultFraction print: 1];
        
        [aFraction print: 0];
        NSLog (@"/");
        [bFraction print: 0];
        NSLog (@"=");
        
        resultFraction = [aFraction divide:bFraction];
        [resultFraction print: 1];
        
    
        
    }
    
    return 0;
}

@end


Last Edit: June 08, 2012, 06:39:53 AM by stuartjj Logged
duducaribeiro
Newbie
*
Posts: 4


Email




Reply #3 on: June 20, 2012, 05:18:08 PM

there is a missing : in line 29

It reads
-(void) print (BOOL) reduceIfNecessary 

It should read
-(void) print: (BOOL) reduceIfNecessary 
Logged
Ahmed Al H
Newbie
*
Posts: 11







Reply #4 on: July 17, 2012, 08:21:52 PM

In Main
Code: (Objective-C)
[resultFraction print:YES];

@interface
Code: (Objective-C)
-(void)       print: (BOOL) wantedReduced;

@implementation
Code: (Objective-C)
-(void)       print: (BOOL) wantedReduced;
{
    Fraction *result = [[Fraction alloc] init];
   
    wantedReduced ? (result.numerator = numerator, result.denominator = denominator), [result reduce], NSLog(@"%i/%i", result.numerator, result.denominator):NSLog(@"%i/%i", numerator, denominator);
}
Logged
kefka
Newbie
*
Posts: 14


Email




Reply #5 on: August 02, 2012, 09:21:44 AM

Hi All,

my last post seems to have disappeared somehow so trying again. I am posting my print: method below. Can somebody tell me if this is correct and if I am using 'self' correctly in this case?

Code: (Objective-C)
-(void) print: (BOOL) shouldReduce
{
    if (shouldReduce == YES) {
        Fraction *tempFraction = self;
        [tempFraction reduce];
        NSLog(@"%i / %i  REDUCED", tempFraction.numerator, tempFraction.denominator);
    } else {
        NSLog(@"%i / %i  NOT REDUCED", numerator, denominator);
    }
}


Regards

Daniel
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.