Whats going on in valueOfAssets method?


#1

Its been a week and I am stumped on what is going on in this method?? Am I the only one who cannot work out how each employee is getting multiples of 17 in their asset value?? Ive read the chapter over and over and cannot find any information in relation to this method.
Please can someone shine some light on this as I feel I have wasted a week of study :frowning: and I am beginning to feel silly…

-(unsigned int) valueOfAssets { unsigned int sum = 0; for (Asset *a in assets) { sum += [a resaleValue]; } return sum; }


#2

You can use diagnostic statements to find out what’s happening inside that method:

-(unsigned int) valueOfAssets 
{    
    unsigned int count = 0;
    unsigned int sum = 0; 
    for (Asset *a in assets) 
    { 
        count += 1;
        NSLog (@"%d Resale value: %d", count, [a resaleValue]);  // resaleValue constant?
        sum += [a resaleValue];
    }
    NSLog (@"%d Value of assets: %d", count, sum);  // count ?= 17

    return  sum;
}

If you are still unable to diagnose, please post you code (all of it).


#3

Thanks for the diagnostic technique ibex10. Ive been thinking about this for 2 days now trying to work it out before asking for more advice but I need answers please:

This is the first part of the output Im getting using the code exactly as it is in the book as well as the diagnostic technique you informed me of and Im stuck at this point because I can not figure out this:
2013-11-05 17:05:21.251 PeopleProgram 001[957:303] 0 Value of assets: 0 <<< I GET THIS PART. ITS TOTALLY LOGICAL THAT BOTH ARE ‘0’.
2013-11-05 17:05:21.251 PeopleProgram 001[957:303] 1 Resale value: 153 [color=#FF0000] <<< RIGHT HERE! WHERE IS 153 BEING RETURNED FROM??[/color]
2013-11-05 17:05:21.252 PeopleProgram 001[957:303] 1 Value of assets: 153
2013-11-05 17:05:21.252 PeopleProgram 001[957:303] 1 Resale value: 119 [color=#FF0000]<<< HERE! 119?? HOW? AND WHY IS IT STILL ON ‘COUNT 1’?[/color]
2013-11-05 17:05:21.252 PeopleProgram 001[957:303] 1 Value of assets: 119
2013-11-05 17:05:21.253 PeopleProgram 001[957:303] 1 Resale value: 0[color=#FF0000] <<< AND WHY IS THIS ‘0’ AGAIN AND ‘COUNT’ IS STILL 1???[/color]
2013-11-05 17:05:21.253 PeopleProgram 001[957:303] 2 Resale value: 68
2013-11-05 17:05:21.254 PeopleProgram 001[957:303] 2 Value of assets: 68
2013-11-05 17:05:21.254 PeopleProgram 001[957:303] 0 Value of assets: 0
2013-11-05 17:05:21.255 PeopleProgram 001[957:303] 1 Resale value: 51
2013-11-05 17:05:21.255 PeopleProgram 001[957:303] 2 Resale value: 85
2013-11-05 17:05:21.255 PeopleProgram 001[957:303] 2 Value of assets: 136
2013-11-05 17:05:21.256 PeopleProgram 001[957:303] 1 Resale value: 17
2013-11-05 17:05:21.256 PeopleProgram 001[957:303] 2 Resale value: 102
2013-11-05 17:05:21.257 PeopleProgram 001[957:303] 2 Value of assets: 119
2013-11-05 17:05:21.257 PeopleProgram 001[957:303] 1 Resale value: 34
2013-11-05 17:05:21.258 PeopleProgram 001[957:303] 1 Value of assets: 34
2013-11-05 17:05:21.258 PeopleProgram 001[957:303] 0 Value of assets: 0
2013-11-05 17:05:21.259 PeopleProgram 001[957:303] 1 Resale value: 136
2013-11-05 17:05:21.259 PeopleProgram 001[957:303] 1 Value of assets: 136
2013-11-05 17:05:21.259 PeopleProgram 001[957:303] Employees: ( …etc etc etc etc >>>

Then, further down the output Ive got this:
2013-11-05 17:34:39.102 PeopleProgram 001[989:303] Giving up ownership of one employee
2013-11-05 17:34:39.103 PeopleProgram 001[989:303] 1 Resale value: 51 [color=#FF0000]<< How come the ‘valueOfAsset’ (overiden with the diagnostic technique) method is called again at this point? What calls it?[/color]
2013-11-05 17:34:39.103 PeopleProgram 001[989:303] 2 Resale value: 85
2013-11-05 17:34:39.104 PeopleProgram 001[989:303] 2 Value of assets: 136
2013-11-05 17:34:39.104 PeopleProgram 001[989:303] deallocating <Employee 5: $136 in assets> 1.30
2013-11-05 17:34:39.105 PeopleProgram 001[989:303] deallocating <Laptop 3: $51 >
2013-11-05 17:34:39.105 PeopleProgram 001[989:303] deallocating <Laptop 5: $85 >
2013-11-05 17:34:39.106 PeopleProgram 001[989:303] Giving up ownership of array …etc etc etc >>>

Its so horrible being stuck like this. Its like being stuck in overnight traffic and I hope it doesn’t happen again because its so draining :cry:
please advise me ibex10


#4

[quote]2013-11-05 17:05:21.251 PeopleProgram 001[957:303] 0 Value of assets: 0 <<< I GET THIS PART. ITS TOTALLY LOGICAL THAT BOTH ARE ‘0’.
2013-11-05 17:05:21.251 PeopleProgram 001[957:303] 1 Resale value: 153 <<< RIGHT HERE! WHERE IS 153 BEING RETURNED FROM??
2013-11-05 17:05:21.252 PeopleProgram 001[957:303] 1 Value of assets: 153[/quote]
[a resaleValue] in:

NSLog (@"%d Resale value: %d", count, [a resaleValue]);

Go check the resaleValue method to see how it computes the value it returns.

Please post your code (all of it) if you still need help.


#5

But ibex10, ‘resaleValue’ is not a method? Its a property of type ‘int’ :

@property unsigned int resaleValue;

So how would it ‘compute’ a value? It would only ever return the value assigned to it, right…

NSLog (@"%d Resale value: %d", count, [a resaleValue]); <<< ’ [a resaleValue] ’ returns the ‘resaleValue’ of ‘a’ and ‘a’ is an ‘Asset in assets’ so at what point would it compute anything??
This cant be??? What am I missing??!
:confused: :confused:


#6

oh God, I think I’ve got it… :open_mouth: Let me be sure.

I’ll be back!!


#7

Here we go:
I NSlog(ged) the results of the random numbers generated and it started to make sense:

[code]NSUInteger randomIndex = random() % [employees count]; // Get a random number between 0-9 inclusive.

// This is my Log to see what numbers are generated. The numbers between 0-9 that were not generated are the numbers of the person’s that didn’t get an asset. In this case 0, 4 and 8 were not generated so Employee’s 0, 4 and 8 have no asset’s.
NSLog(@“Number generated by random function : %lu”, randomIndex); [/code]

And then something hit me:
i * 17… doh!

I totally understand what is going on now! Its amazing how sometimes you “expect” things to be so different to what they actually are.
Im so glad to be out of that black hole but I am ready for the next one…
Ibex10 thanks for not giving the answer away and making me think about what is going on instead… but just one question… Why did you refer to ‘resaleValue’ as a ‘method’ in your last post?


#8

Excellent work!

As for the resaleValue, you are right it is a property. I though it was a method because I did not look at the code in the book carefully.