Amazon.com Widgets program 9.1 I really can't explain this output
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 20, 2014, 08:02:52 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
| | |-+ program 9.1 I really can't explain this output
Pages: [1] Go Down
Print
Author Topic: program 9.1 I really can't explain this output (Read 2871 times)
Ferex
Newbie
*
Posts: 12


Email




on: March 22, 2012, 12:02:07 PM

Hi, this is my first post, I am new to programming but with good will I achieved to 9th chapter but now I have a problem I can't explain, I get no result for the sum of the two fractions instances.
This is the code:

// Interface file for Complex class

#import <Foundation/Foundation.h>

@interface Complex : NSObject

@property double real, imaginary;
-(void) print;
-(void) setReal: (double) a andImaginary: (double) b;
-(Complex *) add: (Complex *) f;

@end



#import <Foundation/Foundation.h>

//define the Fraction class

@interface Fraction : NSObject

@property int numerator, denominator;

-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(Fraction *) add: (Fraction *) f;
-(void) reduce;

@end



//implementation file for Complex class

#import "Complex.h"

@implementation Complex

@synthesize real, imaginary;

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

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

-(Complex *) add:(Complex *)f
{
    Complex *result = [[Complex alloc] init];
    result.real = real + f.real;
    result.imaginary = imaginary + f.imaginary;
    return result;
}
@end



#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
{
    numerator = numerator * f.denominator + denominator * f.numerator;
    denominator = denominator * f.denominator;
    [self reduce];
    return 0;
}

-(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
// shared method name: Polymorphism


#import "Fraction.h"
#import "Complex.h"

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        Fraction *f1 = [[Fraction alloc] init];
        Fraction *f2 = [[Fraction alloc] init];
        Fraction *fracResult;
        Complex *c1 = [[Complex alloc] init];
        Complex *c2 = [[Complex alloc] init];
        Complex *compResult;
       
        [f1 setTo:1 over:10];
        [f2 setTo:2 over:15];
       
        [c1 setReal:18.0 andImaginary:2.5];
        [c2 setReal:-5 andImaginary:3.2];
       
        //add and print 2 complex numbers
        [c1 print]; NSLog(@"     +"); [c2 print];
        NSLog(@"---------");
        compResult = [c1 add: c2];
        [compResult print];
        NSLog(@"\n");
       
        //add and print 2 fractions
        [f1 print]; NSLog(@"   +"); [f2 print];
        NSLog(@"----");
        fracResult = [f1 add: f2];
        [fracResult print];
       
    }
    return 0;
}
Logged
mitchb
Full Member
***
Posts: 144






Reply #1 on: March 22, 2012, 11:32:48 PM

Your Fraction -add method has a return 0.

-(Fraction *) add: (Fraction *) f
{
    numerator = numerator * f.denominator + denominator * f.numerator;
    denominator = denominator * f.denominator;
    [self reduce];
    return 0; <--- need to return a (Fraction *).
}

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
Ferex
Newbie
*
Posts: 12


Email




Reply #2 on: March 27, 2012, 10:32:19 AM

Many thanks, it worked!
Logged
Ferex
Newbie
*
Posts: 12


Email




Reply #3 on: March 27, 2012, 10:38:52 AM

Sorry for the off topic but, how can I make the code I copy/paste in the forum appear with the beautiful interface I often see here?
Logged
wcham
Newbie
*
Posts: 2


Email




Reply #4 on: April 02, 2012, 04:12:06 PM

When you post you need to surround it the word code with brackets then close it with a /code(in brackets too.

or hit the little button under "BCC tags" that has a hastag(#) symbol. Then insert your code in there.
Logged
gosborne3
Newbie
*
Posts: 3


Email




Reply #5 on: June 15, 2014, 12:34:12 PM

Hi Everyone,

Code for the first question stumped me at first but I worked it through and I came up with this:

main.m

#import "Fraction.h"
#import "Complex.h"

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
      Fraction *f1 = [[Fraction alloc] init];
      Fraction *f2 = [[Fraction alloc] init];

        Complex *c1 = [[Complex alloc] init];
        Complex *c2 = [[Complex alloc] init];
        Complex *compResult;
     
       
        [c1 setReal:18.0 andImaginary:2.5];
        [c2 setReal:-5 andImaginary:3.2];
       
        //add and print 2 complex numbers
        [c1 print]; NSLog(@"     +"); [c2 print];
        NSLog(@"---------");
        compResult = [c1 add: c2];
        [compResult print];
        NSLog(@"\n");
       
        [f1 setTo: 1 over: 10];
        [f2 setTo: 2 over: 15];
       
        [f1 print];
        NSLog(@"+");
        [f2 print];
        NSLog(@"=");
       
        [f1 add: f2];
        [f1 reduce];
        [f1 print];
       
    }
    return 0;
}

Fraction.h:

#import <Foundation/Foundation.h>

//define the Fraction class

@interface Fraction : NSObject

@property int numerator, denominator;

-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f;
-(void) reduce;


@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;
   
}
-(double) convertToNum
{
    if (denominator !=0)
        return (double) numerator/denominator;
    else
        return NAN;
   
}
-(void) add: (Fraction *) f

{
   
    numerator = numerator * f.denominator + denominator * f.numerator;
    denominator= denominator * f.denominator;
   
   
}
-(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

Complex.h:

#import <Foundation/Foundation.h>

@interface Complex : NSObject

@property double real, imaginary;
-(void) print;
-(void) setReal: (double) a andImaginary: (double) b;
-(Complex *) add: (Complex *) f;

@end

Complex.m:

#import "Complex.h"

@implementation Complex

@synthesize real, imaginary;

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

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

-(Complex *) add:(Complex *)f
{
    Complex *result = [[Complex alloc] init];
    result.real = real + f.real;
    result.imaginary = imaginary + f.imaginary;
    return result;
}
@end

BTW, thanks for all the good ideas and code examples, it really helps.

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