Amazon.com Widgets Program 3.4 - why does my error give this result?
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 18, 2014, 12:28:51 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
| |-+ Chapter 3
| | |-+ Program 3.4 - why does my error give this result?
Pages: [1] Go Down
Print
Author Topic: Program 3.4 - why does my error give this result? (Read 831 times)
Bunchadna
Newbie
*
Posts: 27






on: April 02, 2012, 07:58:50 PM

Here's the interface section as it should be:

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator:   (int)   n;
-(void) setDenominator:   (int)   d;
-(int) numerator;
-(int) denominator;

@end


But if I replace (int) with (void) in only the two spots above (and not make that same mistake later in the program), like so:

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator:   (int)   n;
-(void) setDenominator:   (int)   d;
-(void) numerator;
-(void) denominator;

@end


I get the following output
The value of myFraction is:  181475088/3

The denominator is correct, but the numerator is some random number that changes every time I run the program (the program builds successfully, but gives me a couple of warning messages).
Example output:
The value of myFraction is:  154222880/3
The value of myFraction is:  200360224/3
The value of myFraction is:  51462512/3

This really isn't all that important, I was just thinking that understanding the output will help me better understand some of the nuances of the language, or how the compiler functions.

Thanks.
Here's a screen shot of the "mistake" program:


Last Edit: April 02, 2012, 08:05:49 PM by Bunchadna Logged
jonr
Full Member
***
Posts: 142






Reply #1 on: April 10, 2012, 02:19:42 AM

I sort of cracked up when I saw what you did as I used to do the same type of stuff on purpose when I was studying C.  My main reason was to try and 'break stuff' or 'break the rules' in order to understand how things work.  I noticed that 'deconstructing' stuff really helps in understanding how something works.  This applies to other disciplines as well--for instance music theory.

I'm probably not going to be able to give you a fully coherent answer as I'm a beginner, and I know just enough C to be dangerous.  But I think that this is a C issue having to do with data types; and I've seen this before when I was studying C.  When you fail to put (int) as the return type when declaring the method (or specifically using (void) which specifies nothing is returned), and then you use that method later (calling for an int to be returned) , the stuff you get back is just random crap floating around in memory as the compiler hasn't been told the correct info.  As far as why the numerator gets affected by this but not the denominator, I have no idea and am now curious myself.  Smiley
Logged
seerex
Full Member
***
Posts: 177


Email




Reply #2 on: April 10, 2012, 04:46:03 AM

How are you printing the values to the console? if you are using the print method, it should still work as expected.

Now, i see you only changed the declaration to an void, and not the implementation section to void, yes? So they still say they return an int?

I still can't give you a good answer though, but i think it's the same concept as other "rule-breaking" stuff. It simply gives you unreliable and unexpected outputs. As to why the denominator is o.k i don't know, but maybe it has to do with some compiler stuff that after messing the numerator up, automatically changes the denominator method to return an int in the .h file.
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
rue
Jr. Member
**
Posts: 53






Reply #3 on: April 21, 2012, 10:19:15 PM

Hello, still newbie here but I've watched/tried some IOS programming by following iTunes U courses.

My understanding is "set" and "get" are the norm/convention to use when setting/getting variables in objects.

The following code works:


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

// Start of @interface section
@interface Fraction: NSObject
{
    int numerator;
    int denominator;
}

- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
- (int) getNumerator;
- (int) getDenominator;

@end // @interface

// Start of @implementation section
@implementation Fraction
- (void) print {
    NSLog(@"%i/%i",numerator,denominator);
}

- (void) setNumerator:(int)n {
    numerator = n;
}

- (void) setDenominator:(int)d{
    denominator = d;
}

- (int) getNumerator {
    NSLog(@"%i",numerator);
    return numerator;
}

- (int) getDenominator {
    NSLog(@"%i",denominator);
    return denominator;
}

@end // @implementation


// Start of main program section

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

    @autoreleasepool {
        Fraction *myFraction;       // create object variable myFraction
        int num, den;
           
        // Create an instance of the object Fraction
        myFraction = [Fraction alloc];
        // myFraction = [Fraction init];        -- no need to init if we're using Xcode 4.3.2
       
       
        // assign value to fraction
        [myFraction setNumerator: 1];
        [myFraction setDenominator:3];
       
        // Display fraction object
        [myFraction print];
        num = [myFraction getNumerator];
        den = [myFraction getDenominator];
       
        NSLog(@"The numerator is %i, and denominator is %i",num,den);
       
       
        // free up object
        [myFraction release];
       
        // [pool drain];   -- no need to pool drain if we're using Xcode 4.3.2

    }
    return 0;
}
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.