Different approach to chapter


#1

I kept going over this chapter and each time I got more confused. Then I read somebody’s post on a thread below mentioning that the “Object Instance Variable” lesson might be being obscured by the “dealloc” and “description” stuff, so I went through the chapter and did a version of the BMITime program without the description and dealloc stuff (and using RIDICULOUSLY specific variable names, to minimize the possibility of confusion) and I finally understood what was going on! For the benefit of anybody who might be helped, I’ll include all my code here.

(For the folks writing the book, which I AM LOVING–I’m actually learning how to do this, as opposed to the other four or five books I’ve read, which have left me frustrated and feeling stupid–if there is another edition, what would you think about putting the dealloc and description stuff in a separate chapter from the object instance variables stuff? Of course, I haven’t gone through and worked the program out with the dealloc and description stuff yet, so maybe there’s something I’m not understanding, and I haven’t even BEGUN to work on the Challenge yet, so maybe there’s a reason we need both at the same time, but if not, I found this way easier to wrap my head around.)

Here’s Employee.h:

[code]#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;

@interface Employee : Person

{
int employeeID;
NSMutableArray *arrayOfOneEmployeesAssets;

}

@property int employeeID;
@property NSMutableArray *arrayOfOneEmployeesAssets;
-(void)addObjectToArrayOfOneEmployeesAssets:(Asset *)a;
-(int)valueOfOneEmployeesAssets;

@end[/code]

Employee.m:

[code]#import “Employee.h”
#import “Asset.h”

@implementation Employee

@synthesize employeeID,arrayOfOneEmployeesAssets;

-(void)addObjectToArrayOfOneEmployeesAssets:(Asset *)a
{
//Is assets nil?
if (!arrayOfOneEmployeesAssets)
{
arrayOfOneEmployeesAssets = [[NSMutableArray alloc] init];
}

[arrayOfOneEmployeesAssets addObject:a];

}

-(int)valueOfOneEmployeesAssets
{
int sum = 0;
for (Asset *a in arrayOfOneEmployeesAssets)
{
sum+=[a valueOfThisAsset];
}
return sum;
}

-(float) bodyMassIndex
{
float normalBMI = [super bodyMassIndex];
return normalBMI*.9;
}

@end
[/code]

Asset.h:

[code]#import <Foundation/Foundation.h>

@interface Asset : NSObject

{
NSString *nameOfThisAsset;
int valueOfThisAsset;
}

@property (strong) NSString *nameOfThisAsset;
@property int valueOfThisAsset;

@end[/code]

Asset.m:

[code]#import “Asset.h”

@implementation Asset

@synthesize nameOfThisAsset,valueOfThisAsset;

@end[/code]

main.m:

[code]#import <Foundation/Foundation.h>
#import “Employee.h”
#import “Asset.h”

int main(int argc, const char * argv[])
{
@autoreleasepool
{
//First, create an array of employees.
NSMutableArray *listOfEmployees = [[NSMutableArray alloc] init];

    //Fill that array with employees.
    for (int i = 0; i<10; i++)
    {
        //Create an instance of Employee class
        Employee *employee = [[Employee alloc] init];
        
        //Give this employee characteristics via instance variables
        [employee setHeightInMeters:1.8-(i/10.0)];
        [employee setWeightInKilos:90+i];
        [employee setEmployeeID:i];
        
        //Add this employee to the array of employees
        [listOfEmployees addObject];
    }
    
    //Next, create ten assets to be distributed randomly among the employees.
    for (int i = 0; i<10; i++)
    {
        Asset *aSingleAsset = [[Asset alloc] init];

        //Give each asset characteristics via instance variables.
        NSString *labelOfThisAsset = [NSString stringWithFormat:@"asset number %d",i];
        [aSingleAsset setNameOfThisAsset:labelOfThisAsset];
        [aSingleAsset setValueOfThisAsset:i*17];
        
        //Choose a random number to determine which employee gets this asset.
        NSUInteger randomNumber = random() % [listOfEmployees count];
        
        //Designate that employee.
        Employee *employeeToReceiveAsset = [listOfEmployees objectAtIndex:randomNumber];
        
        //Assign that employee the asset.
        [employeeToReceiveAsset addObjectToArrayOfOneEmployeesAssets:aSingleAsset];
        
    }
    
    NSLog(@"Here are your employees.");
    
    for (int i=0; i<[listOfEmployees count]; i++)
    {
        Employee *employeeToExamine = [listOfEmployees objectAtIndex:i];
        NSMutableArray *thisEmployeesAssets = [employeeToExamine arrayOfOneEmployeesAssets];
        NSLog(@"Employee #%d has %lu assets:\n",[employeeToExamine employeeID],[[employeeToExamine arrayOfOneEmployeesAssets] count]);
        if ([[employeeToExamine arrayOfOneEmployeesAssets] count]!=0)
        {
            float sum=0;
            for (Asset *a in thisEmployeesAssets)
            {
                NSLog(@"%@, worth %d\n",[a nameOfThisAsset],[a valueOfThisAsset]);
                sum+=[a valueOfThisAsset];
            }
        NSLog(@"Employee #%d's assets are worth a total of %.2f.",[employeeToExamine employeeID],sum);
        }
    }

return 0;
}

}[/code]


#2

The request is noted, and we will seriously consider it. Thanks!


#3

Thank you for this. The variable name changes made it much clearer. I’m still not 100% on it but I’m going to look at it again with fresh eyes tomorrow.


#4

Your code helped me understand this chapter! Thanks! :smiley:


#5

Great! It is much more clear with the not-so-ridiculously-clear objectnames. I hope it will help me complete the challenge.


#6

Thank you much! Very helpful so far. I think the inheritance lesson was obscured by more than poor naming. Might be better for the authors to drop the BMI base out of this and start from scratch to demonstrate. Also the code for automatically creating assets for the array is good to learn, but for me, definitely obscure the lesson. I reached a breaking point of understanding and was just copying code into Xcode at a certain point which was different from the rest of the chapters so far.

Do you know if our new classes (Employee and Asset) need to have "#import <Foundation/Foundation.h> in the .h file or just their the class they are inheriting from, i.e. #import “Person.h” for Employee?

Seems to work either way. Is it better one way or the other?


#7

I’m having difficulties understanding this chapter, I even went back to previous chapter up to chapter 12 because I taught I was missing something … I even used the debug area to have idea on whats going on with the codes provided on this book. ,this post help me a lot, I really appreciate your post and the way you name your objects and methods … Thanks you so much