Amazon.com Widgets Exercise 1 Chapter 7
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 23, 2014, 12:16:43 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
| | | |-+ Exercise 1 Chapter 7
Pages: [1] Go Down
Print
Author Topic: Exercise 1 Chapter 7 (Read 1825 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: January 21, 2012, 04:49:59 PM

Code: (Objective-C)

#import <Foundation/Foundation.h>

// The Fraction class
// This one goes in Fraction.h file:


@interface Fraction : NSObject

@property int numerator, denominator;




-(void) print;

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

-(double) converToNum;

-(Fraction *) add: (Fraction*) f;

-(Fraction *) divide: (Fraction*) f;

-(Fraction *) subtract: (Fraction*) f;

-(Fraction *) multiply: (Fraction*) f;

-(void) reduce;
@end

// This one goes in Fraction.m file:

#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;


-(void) print;
{
    NSLog(@"%i/%i", numerator, denominator);
}
-(double) converToNum;
{
    if (denominator !=0)
        return (double) numerator / denominator;
    else
    return NAN;
        
}
-(void) setTo:(int)n over:(int)d;
{
    numerator = n;
    denominator = d;
}

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

- (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 *) divide:(Fraction *)f;
{
    Fraction *result = [[ Fraction alloc] init];
    
    result.numerator = numerator /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;
}

@end

// This one goes in main.m file:

#import "Fraction.h"

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

    @autoreleasepool
    {
        int n1,d1,n2,d2;
        
        NSLog(@"Type in your first numerator:");
        scanf ("%i", &n1);
        
        NSLog(@"Type in your first denominator:");
        scanf ("%i", &d1);
        
        NSLog(@"Type in your second numerator:");
        scanf ("%i", &n2);
        
        NSLog(@"Type in your second denominator:");
        scanf ("%i", &d2);
        
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
        
        Fraction *resultFraction;
        
        [aFraction setTo:n1 over:d1];
        [bFraction setTo:n2 over:d2];
        
        resultFraction = [aFraction add:bFraction];
        [resultFraction print];
      
        resultFraction = [aFraction divide:bFraction];
        [resultFraction print];
        
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print];
        
        resultFraction = [aFraction multiply:bFraction];
        [resultFraction print];
    }
    return 0;
}


Last Edit: February 21, 2012, 03:12:00 PM by alexsom Logged
stan4ev
Newbie
*
Posts: 7


Email




Reply #1 on: February 21, 2012, 01:24:14 PM

This is not right!
I only checked your subtract method and this is not right!
you can not subtract fractions the same way as you add them!
Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #2 on: February 21, 2012, 03:25:02 PM

I see, you got confused with these ones:
 [aFraction print]; 
 NSLog(@" + "); 
  [bFraction print]; 
  NSLog(@" = ");

I just forgot to take them off before posting it.
Logged
stan4ev
Newbie
*
Posts: 7


Email




Reply #3 on: February 21, 2012, 04:28:36 PM

did you actually tested it?
because I have done something similar and it is not giving me right results
even on the adding which is given
if you add 1/4 + 1/2 it gives you 3/4 which is right but when you do 3/4 + 1/2 = 1/1 which is wrong, it should be 5/4
and on the subtracting
1/4 - 1/2 = 0/1 which is rubish
1/4 * 1/2 = 0/1
1/4 / 1/2 0/1

please help !!!

this is my code for the program

main.m

#import "Fraction.h"

int main (int argc, const char * argv[])
{
   
    @autoreleasepool
    {
        int n1,n2,d1,d2;
       
        NSLog(@"Type in your first fraction:"); 
        scanf ("%i/%i", &n1, &d1); 
           
        NSLog(@"Type in your second fraction:"); 
        scanf ("%i/%i", &n2, &d2); 
       
       
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
       
        Fraction *resultFraction;
       
        [aFraction setTo:n1 over:d1];
        [bFraction setTo:n2 over:d2];
       
        [aFraction print];
        NSLog(@"+");
        [bFraction print];
        NSLog(@"=");
        resultFraction = [aFraction add:bFraction];
        [resultFraction print];
       
        [aFraction print];
        NSLog(@"-");
        [bFraction print];
        NSLog(@"=");
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print];
       
        [aFraction print];
        NSLog(@"*");
        [bFraction print];
        NSLog(@"=");
        resultFraction = [aFraction multiply:bFraction];
        [resultFraction print];
       
        [aFraction print];
        NSLog(@"/");
        [bFraction print];
        NSLog(@"=");
        resultFraction = [aFraction divide:bFraction];
        [resultFraction print];
       
    }
    return 0;
}

Fraction.h


#import <Foundation/Foundation.h>

//the fraction class
@interface Fraction : NSObject

@property int numerator, denominator;

-(void) print;

-(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;

-(double) convertToNum;



@end

Fraction.m


#import "Fraction.h"


@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
    NSLog(@"%i/%i", numerator, denominator);
}

-(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 reduce];
   
    return result;
   
}

-(Fraction *) subtract:(Fraction *)f
{
    //to subtract two fraction
       
     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 = v % u;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator /= u;
}

-(double) convertToNum
{
    if (denominator != 0)
       
        return (double) numerator/denominator;
    else
        return NAN;   
}


@end
Logged
stan4ev
Newbie
*
Posts: 7


Email




Reply #4 on: February 21, 2012, 05:41:08 PM

thanks for that its ok now!
but the divide method is right

a/b / c/d = a*d/b*c
this is how you divide fractions the first fraction multiplied by the reciprocal of the second Smiley

thanks for your help!!!
Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #5 on: February 21, 2012, 05:56:24 PM

right  Embarrassed
Logged
jacqueline
Newbie
*
Posts: 7






Reply #6 on: March 01, 2012, 02:00:58 AM

Here is my try Smiley

#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

- (void) print
{
    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
{
    //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 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;   
}

- (Fraction *) minus:(Fraction *) f
{
    //To minus one fraction from another
   
    Fraction *result = [[Fraction alloc] init];
   
    //Find the greatest common denominator
   
    int p;
   
    p = denominator * f.denominator;
   
   
    result.numerator = ((p / denominator) * numerator) - ((p /f.denominator) * f.numerator);
    result.denominator = p;
   
    [result reduce];
    return result;
   
}

- (Fraction *) multiply:(Fraction *) f;
{
    //To multipy one fraction by another
   
    Fraction *result = [[Fraction alloc] init];
   
    //Find the greatest common denominator
   
    int p;
   
    p = denominator * f.denominator;
   
   
    result.numerator = (numerator * f.numerator);
    result.denominator = p;
   
    [result reduce];
    return result;
   
}

- (Fraction *) divide:(Fraction *) f;
{
    //to divide one fraction by another
   
    Fraction *result = [[Fraction alloc] init];

    result.numerator = numerator * f.denominator;
    result.denominator = denominator * f.numerator;

    [result reduce];
    return result;


}


And the results :
2012-03-01 16:57:20.540 Excercise 7_1[7164:707] 1/2
2012-03-01 16:57:20.543 Excercise 7_1[7164:707] +
2012-03-01 16:57:20.547 Excercise 7_1[7164:707] 1/6
2012-03-01 16:57:20.547 Excercise 7_1[7164:707] =
2012-03-01 16:57:20.548 Excercise 7_1[7164:707] 2/3
2012-03-01 16:57:20.548 Excercise 7_1[7164:707]  -----------------------------
2012-03-01 16:57:20.549 Excercise 7_1[7164:707] 1/2
2012-03-01 16:57:20.549 Excercise 7_1[7164:707] -
2012-03-01 16:57:20.550 Excercise 7_1[7164:707] 1/6
2012-03-01 16:57:20.550 Excercise 7_1[7164:707] =
2012-03-01 16:57:20.551 Excercise 7_1[7164:707] 1/3
2012-03-01 16:57:20.551 Excercise 7_1[7164:707]  -----------------------------
2012-03-01 16:57:20.551 Excercise 7_1[7164:707] 1/2
2012-03-01 16:57:20.552 Excercise 7_1[7164:707] *
2012-03-01 16:57:20.552 Excercise 7_1[7164:707] 1/6
2012-03-01 16:57:20.552 Excercise 7_1[7164:707] =
2012-03-01 16:57:20.553 Excercise 7_1[7164:707] 1/12
2012-03-01 16:57:20.553 Excercise 7_1[7164:707]  -----------------------------
2012-03-01 16:57:20.553 Excercise 7_1[7164:707] 1/2
2012-03-01 16:57:20.554 Excercise 7_1[7164:707] /
2012-03-01 16:57:20.554 Excercise 7_1[7164:707] 1/6
2012-03-01 16:57:20.557 Excercise 7_1[7164:707] =
2012-03-01 16:57:20.558 Excercise 7_1[7164:707] 3/1
Program ended with exit code: 0
Logged
clouded
Full Member
***
Posts: 123






Reply #7 on: May 14, 2012, 09:40:39 AM

Here's what I have, it looks very similar to many, but the results seem right:

Code: (Objective-C)
// Chapter 7 Exercise 1.
// Add the following methods to the Fraction class to round out the arithmetic
// operations on fractions. Reduce the result within the method in each case:
// Subtract argument from receiver
//     (Fraction *) subtract: (Fraction *) f;
// Multiply receiver by argument
//     (Fraction *) multiply: (Fraction *) f;
// Divide receiver by argument
//     (Fraction *) divide: (Fraction *) f;
//

 
// 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) reduce;
@end

// Fraction.m

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

-(void) print
{
    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

// 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: 1 over: 4]; // set 1st fraction to 1/4
        [bFraction setTo: 1 over: 2]; // set 2nd fraction to 1/2
        
        [aFraction print];
        NSLog (@"+");
        [bFraction print];
        NSLog (@"=");
        
        resultFraction = [aFraction add: bFraction];
        [resultFraction print];
        NSLog(@"w");
        [aFraction setTo: 1 over: 4]; // set 1st fraction to 1/4
        
        [aFraction print];
        NSLog (@"-");
        [bFraction print];
        NSLog (@"=");
        
        resultFraction = [aFraction subtract: bFraction];
        [resultFraction print];
        NSLog(@"w");
        [aFraction setTo: 1 over: 4]; // set 1st fraction to 1/4
        
        [aFraction print];
        NSLog (@"x");
        [bFraction print];
        NSLog (@"=");
        
        resultFraction = [aFraction multiply: bFraction];
        [resultFraction print];
        NSLog(@"w");
        [aFraction setTo: 1 over: 4]; // set 1st fraction to 1/4
        
        [aFraction print];
        NSLog (@"/");
        [bFraction print];
        NSLog (@"=");
        
        resultFraction = [aFraction divide: bFraction];
        [resultFraction print];
        
    }
    return 0;
}

Output:

1/4
+
1/2
=
3/4
w
1/4
-
1/2
=
1/-4
w
1/4
x
1/2
=
1/8
w
1/4
/
1/2
=
1/2

the 'w' are supposed to headings, i forgot, got lazy
Last Edit: May 14, 2012, 12:17:32 PM by clouded Logged
kefka
Newbie
*
Posts: 14


Email




Reply #8 on: July 25, 2012, 05:57:42 AM

The hardest part of this was actually researching fraction math, its been a while!! Here is my code:

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

// The Fraction class

@interface Fraction : NSObject

@property int numerator, denominator;

-(void) print;
-(void) setTo: (int) n over: (int) d;
-(void) reduce;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f; // Subtract argument from receiver
-(Fraction *) multiply: (Fraction *) f; // Multiply receiver by argument
-(Fraction *) divide: (Fraction *) f;   // Divide receiver by argument


@end

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

@implementation Fraction

@synthesize numerator, denominator;




-(void) print
{
    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;
    }

// add a Fraction to the receiver

-(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 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

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

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

    @autoreleasepool {
        Fraction *aFraction = [[Fraction alloc]init];
        Fraction *bFraction = [[Fraction alloc]init];
       
        Fraction * resultFraction;
       
       
        [aFraction setTo:1 over:4];//Set first fraction to 1/4
        [bFraction setTo:1 over:2];//Set the second one to 1/2
       
        // Display the fraction
        NSLog(@"ADDING FRACTIONS");
        [aFraction print];
        NSLog(@"+");
        [bFraction print];
        NSLog(@"=");
       
      resultFraction = [aFraction add:bFraction];
        [resultFraction print];
       
        NSLog(@"SUBTRACTING FRACTIONS");
        [aFraction print];
        NSLog(@"-");
        [bFraction print];
        NSLog(@"=");
       
        resultFraction = [aFraction subtract:bFraction];
        [resultFraction print];
       
        NSLog(@"MULTIPLYING FRACTIONS");
        [aFraction print];
        NSLog(@"*");
        [bFraction print];
        NSLog(@"=");
       
        resultFraction = [aFraction multiply:bFraction];
        [resultFraction print];
       
        NSLog(@"DIVIDING FRACTIONS");
        [aFraction print];
        NSLog(@"/");
        [bFraction print];
        NSLog(@"=");

        resultFraction = [aFraction divide:bFraction];
        [resultFraction print];
    }
    return 0;
}


OUTPUT


2012-07-25 13:51:11.995 FractionTest[8493:403] ADDING FRACTIONS
2012-07-25 13:51:11.997 FractionTest[8493:403] 1/4
2012-07-25 13:51:11.998 FractionTest[8493:403] +
2012-07-25 13:51:11.998 FractionTest[8493:403] 1/2
2012-07-25 13:51:11.999 FractionTest[8493:403] =
2012-07-25 13:51:11.999 FractionTest[8493:403] 3/4
2012-07-25 13:51:12.000 FractionTest[8493:403] SUBTRACTING FRACTIONS
2012-07-25 13:51:12.000 FractionTest[8493:403] 1/4
2012-07-25 13:51:12.001 FractionTest[8493:403] -
2012-07-25 13:51:12.001 FractionTest[8493:403] 1/2
2012-07-25 13:51:12.001 FractionTest[8493:403] =
2012-07-25 13:51:12.002 FractionTest[8493:403] 1/-4
2012-07-25 13:51:12.002 FractionTest[8493:403] MULTIPLYING FRACTIONS
2012-07-25 13:51:12.003 FractionTest[8493:403] 1/4
2012-07-25 13:51:12.003 FractionTest[8493:403] *
2012-07-25 13:51:12.004 FractionTest[8493:403] 1/2
2012-07-25 13:51:12.004 FractionTest[8493:403] =
2012-07-25 13:51:12.004 FractionTest[8493:403] 1/8
2012-07-25 13:51:12.005 FractionTest[8493:403] DIVIDING FRACTIONS
2012-07-25 13:51:12.005 FractionTest[8493:403] 1/4
2012-07-25 13:51:12.006 FractionTest[8493:403] /
2012-07-25 13:51:12.006 FractionTest[8493:403] 1/2
2012-07-25 13:51:12.006 FractionTest[8493:403] =
2012-07-25 13:51:12.007 FractionTest[8493:403] 1/2
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.