Amazon.com Widgets Author's mistake ? The same code from the book doesn't work on my Xcode 4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 08:04:58 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 11
| | | |-+  Author's mistake ? The same code from the book doesn't work on my Xcode 4
Pages: [1]   Go Down
Print
Author Topic: Author's mistake ? The same code from the book doesn't work on my Xcode 4  (Read 475 times)
Justwarrior
Newbie
*
Posts: 25


Email




« on: June 26, 2012, 04:15:32 AM »

Hi everyone.

The same code from the book in MathOPS Category and Test Program (Program 11.1), doesn't work on my xcode. I did the same thing...

It says that numerator and denominator are undeclared variables, but it supposed to iherit this variables from the Fraction.h

Is that author's mistake ? What's wrong in this code? :

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

@interface Fraction (MathOps)
-(Fraction *)add:(Fraction *) f;
-(Fraction *)mul:(Fraction *) f;
-(Fraction *)sub:(Fraction *) f;
-(Fraction *)div:(Fraction *) f;

@end

@implementation Fraction (MathOps)
-(Fraction *)add:(Fraction *) f
{
    Fraction *result = [[Fraction alloc]init];
    
    result.numerator = (numerator * f.denominator)+(denominator * f.numerator);

// numerator and denominator: Using undeclared variables.

    result.denominator = denominator * f.denominator;
}
-(Fraction *)mul:(Fraction *) f
{
    Fraction *result = [[Fraction alloc]init];
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;

    // numerator and denominator: Using undeclared variables.
}
-(Fraction *)sub:(Fraction *) f {}
-(Fraction *)div:(Fraction *) f {}
@end

typedef Fraction *FractionObj;
int main (int argc, const char * argv[])
{

    @autoreleasepool {
        complex *imaginary= [[complex alloc]init];
        complex *real = [[complex alloc]init];
        complex *results;
        
        [real setReal:1];
        [real setImaginary:2];
        [real print];
        
        [imaginary setReal:2];
        [imaginary setImaginary:1];
        [imaginary print];
        
        results = [real add: imaginary];
        [results print];
        NSLog(@"FRACTIONS!");
        Fraction *a = [[Fraction alloc]init];
        Fraction *b = [[Fraction alloc]init];
        id resultFraction;
        
        [a setTo:20 over:3];
        [b setTo:1 over:3];
        [b minusSign];
        [a minusSign];
        
        id ab = a;
        id ba = b;
        [ab print: 0];
        NSLog(@"-");
        [ba print: 0];
        NSLog(@"=");
        resultFraction = [ab multiplyN: ba];
        [resultFraction print: 1];
        
      XYPoint *xy = [[XYPoint alloc]init];
        
        [xy setX:100 andY:110];
        [xy print];
        
        NSLog(@"VERIFYING \n \n \n");
        Fraction *fraction = [[Fraction alloc]init];
        complex *Complex = [[complex alloc]init];
        id number = [[complex alloc]init];
        
        if([fraction isMemberOfClass: [complex class]])
            NSLog(@"Fraction is member of class complex");
        if([Complex isMemberOfClass: [NSObject class]])
            NSLog(@"complex is member of class NSObject");
        if([Complex isKindOfClass: [NSObject class]])
            NSLog(@"complex is kind of class NSObject");
        if([fraction isKindOfClass: [Fraction class]])
            NSLog(@"Fraction is kind of class Fraction");
        if([fraction respondsToSelector: @selector(print)])
           NSLog(@"Fraction respond to selector print");
        if([Complex respondsToSelector: @selector(print)])
           NSLog(@"Complex responds to print");
        if([Fraction instancesRespondToSelector: @selector(print)])
           NSLog(@"Fraction's instances respond to print");
        if([number respondsToSelector: @selector (print)])
           NSLog(@"number responds to print");
        if([number isKindOfClass: [complex class]])
           NSLog(@"number is kind of class complex");
        if([[number class] respondsToSelector:@selector (alloc)])
           NSLog(@"Number class responds to alloc");
        
        if([a counter]!=1){ NSLog(@"%i times was invoked method multiplyN", a.counter); }
        else { NSLog(@"Only %i time was invoked method multiplyN", a.counter);}
        
        typedef enum {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday} Day;
          
    }
    
    return 0;
}


Fraction.h

Code: (Objective-C)
 
@interface Fraction : NSObject
@property int numerator, denominator, counter;

//methods
@end

Fraction.m

Code: (Objective-C)
@implementation
@synthesize numerator, denominator, counter;

//methods

@end
« Last Edit: June 26, 2012, 05:33:31 AM by Justwarrior » Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #1 on: June 26, 2012, 05:02:37 AM »

Please, help me!!!  Undecided
Logged
clouded
Full Member
***
Posts: 123






« Reply #2 on: June 26, 2012, 05:25:37 AM »

You're missing code...

"complex.h" 
"Fraction.h" 
"XYPoint.h"

Please post everything once you're done so we can help
... and how are the variables declared? public, private, static, etc...? look into that... might help
« Last Edit: June 26, 2012, 05:27:35 AM by clouded » Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #3 on: June 26, 2012, 05:33:47 AM »

Now I put... Can you check please ?
Logged
clouded
Full Member
***
Posts: 123






« Reply #4 on: June 26, 2012, 06:08:58 AM »

Here's how Program 1 should be set up in your Xcode http://classroomm.com/objective-c/index.php?topic=7326.0 I separated everything for clarification, it's together in the book, either way will work.
« Last Edit: June 26, 2012, 06:30:14 AM by clouded » 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.