After 1 Month I Finally Understand this chapter!


#1

I’ve been stuck on this chapter for the longest time! I even reread the book only to get stuck on chapter 19 again and again. So, I think my main confusion stemmed from how vastly different the code for BMITime is in chapter 19 versus how it was previously done in the book. So I redid chapter 19 BMITime in the same vein as it was previously coded. I really think the dealloc stuff should have been saved for another chapter and the author should have just focused on object instance variables. So I removed it from my code. Also, I would like it if the program built on itself rather than completely recoding it. I understand why it was recoded and it is quite simple for the code to build the assets and employees on its own, rather than doing it manually as I have. Anyways, hopefully this “simpler” version will help others who are stuck. I really hope I’m on the right track now, but if I’m not, please do comment on my code so I know I can go to chapter 20. :slight_smile:

Person.h

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

@interface Person : NSObject
{
// It has two instance variables
float heightInMeters;
float weightInKilos;
}

// This sets the Getter and Setters in one easy line
@property float heightInMeters;
@property float weightInKilos;

// This method calculates the Body Mass Index
-(float) bodyMassIndex;

@end[/code]

Person.m

[code]#import “Person.h”

@implementation Person

// This implements getter and setters in one easy line
@synthesize heightInMeters;
@synthesize weightInKilos;

  • (float) bodyMassIndex {
    return [self weightInKilos] / ([self heightInMeters] * [self heightInMeters]);
    }

@end[/code]

Employee.h

[code]#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset; //Tells the compiler “There is a class called Asset. Don’t panic when you see it in this file. That’s all you need to know for now.” @class gives the compiler less information, but makes the processing of this particular file faster.

@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;
}

@property int employeeID;

-(void) addAssetsObject: (Asset *)a; //Declare a method called “addAssetsObject” that accepts a pointer to an instance object of “Asset” called “a”
-(int) valueOfAssets;

@end[/code]

Employee.m

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

@implementation Employee

@synthesize employeeID;

-(void) addAssetsObject:(Asset *)a //Declare a method called “addAssetsObject” that accepts a pointer to an instance object of “Asset” called “a”
{
//Is assets nil?
if (!assets) {
//Create the array
assets = [[NSMutableArray alloc] init];
}
[assets addObject:a];
}

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

// Overrides the bodyMassIndex method in Person.m class
-(float) bodyMassIndex
{
float normalBMI = [super bodyMassIndex];
return normalBMI * 0.9;
}

@end[/code]

Asset.h

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

@interface Asset : NSObject
{
NSString *label;
int resaleValue;
}

@property (strong) NSString *label; // This is a pointer to an object upon which Asset class claims ownership (strong)
@property int resaleValue;

@end[/code]

Asset.m

[code]#import “Asset.h”

@implementation Asset

@synthesize label;
@synthesize resaleValue;

@end[/code]

main.m

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

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

@autoreleasepool {

    // Create an instance of Employee
    Employee *lyn = [[Employee alloc] init];
    Employee *bob = [[Employee alloc] init];
    
    // Give the instance variables interesting values
    [bob setWeightInKilos:68.0388555];
    [bob setHeightInMeters:1.6764];
    [bob setEmployeeID:1];
    
    [lyn setWeightInKilos:48.5343836];
    [lyn setHeightInMeters:1.5748];
    [lyn setEmployeeID:2];
    
    // Create Assets
    Asset *asset1 = [[Asset alloc] init];
    Asset *asset2 = [[Asset alloc] init];
    Asset *asset3 = [[Asset alloc] init];
    Asset *asset4 = [[Asset alloc] init];
    
    // Give the instance variables interesting values
    [asset1 setLabel:@"Laptop 1"];
    [asset1 setResaleValue:5];
    [asset2 setLabel:@"Laptop 2"];
    [asset2 setResaleValue:10];
    [asset3 setLabel:@"Laptop 3"];
    [asset3 setResaleValue:15];
    [asset4 setLabel:@"Laptop 4"];
    [asset4 setResaleValue:20];
    
    // Assign asset to employee
    [bob addAssetsObject:asset1];
    [bob addAssetsObject:asset2];
    [lyn addAssetsObject:asset3];
    [lyn addAssetsObject:asset4];
    
    NSMutableArray *employee = [NSMutableArray array]; // Empty array
    [employee addObject:bob];
    [employee addObject:lyn];
    
    for (Employee *e in employee){
        NSLog(@"employee #%d (%.2f kilos, %.2f meters) has a BMI of %.2f. Has an asset value of %d", [e employeeID], [e weightInKilos], [e heightInMeters], [e bodyMassIndex], [e valueOfAssets]);
    }
}
return 0;

}[/code]

Output:

employee #1 (68.04 kilos, 1.68 meters) has a BMI of 21.79. Has an asset value of 15
employee #2 (48.53 kilos, 1.57 meters) has a BMI of 17.61. Has an asset value of 35

#2

I’ve started a new project, copied the respective code, and tried to build and run, but you seem to have forgotten some header statements


#3

I’m actually looking at the code again. Can you elaborate? I ran it and it compiled fine. It could be possible that I didn’t copy and paste it into the message.


#4

I see what you mean, errors are fixed. Thank you.