My Solution to Challenge


#1

I didn’t see anyone use self like it was demonstrated in the book. How does this look?

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float) costInDollars;
-(float) valueInDollars;
@end
#import "StockHolding.h"

@implementation StockHolding
@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;

-(float) costInDollars
{
    float pp = [self purchaseSharePrice];
    int ns = [self numberOfShares];
    
    return pp * ns;
}

-(float) valueInDollars
{
    float cs = [self currentSharePrice];
    int ns = [self numberOfShares];
    
    return cs * ns;
}

@end
NSMutableArray *stockArray = [[NSMutableArray alloc] init];
        
        StockHolding *s1 = [[StockHolding alloc] init];
        
        [s1 setCurrentSharePrice:20];
        [s1 setPurchaseSharePrice:13.8];
        [s1 setNumberOfShares: 1500];
        
        StockHolding *s2 = [[StockHolding alloc] init];
        
        [s2 setCurrentSharePrice:41.99];
        [s2 setPurchaseSharePrice:127.99];
        [s2 setNumberOfShares:4599];
        
        StockHolding *s3 = [[StockHolding alloc] init];
        [s3 setCurrentSharePrice:165.99];
        [s3 setPurchaseSharePrice:19.75];
        [s3 setNumberOfShares:243];
        
        [stockArray addObject:s1];
        [stockArray addObject:s2];
        [stockArray addObject:s3];
        
        for(StockHolding *s in stockArray) {
            NSLog(@"current Price is %f, purchased at %f and %i number of shares\n", [s currentSharePrice], [s purchaseSharePrice], [s numberOfShares] );
        }

#2

Looks good but you should also print costInDollars and valueInDollars to see that they really compute their values.

Also you need to be aware that getting the values of instance variables, indirectly, through properties comes at a cost.


#3

[quote=“ibex10”]Looks good but you should also print costInDollars and valueInDollars to see that they really compute their values.

Also you need to be aware that getting the values of instance variables, indirectly, through properties comes at a cost.[/quote]

I omitted the calculations; they look OK.

Regarding instance variable access - are you suggesting what I did was not optimal? I followed the example as well as the statement in the book that alluded to this format being "preferred. Did I misunderstand this?

Thanks.


#4

No, you did not misunderstand it. But you need to be aware that, whenever you access an instance variable through a property you are using more CPU cycles; that means burning more energy. (see Key-Value Observing Programming Guide.)


#5

You don’t need a NSMutableArray for what you’re doing. NSArray is more optimized. If you were creating the stockholdings in a ‘for’ using random values i would agree in using a mutable array.


#6

I am new to programming and this is my first run through of the book. Why did you declare your ivars twice in the .h file. You declared them and then used @property to declare again. Am I correct that it is unnecessary to have both?


#7

wahou i have a lot a difficulties :frowning: but at the end

action.h

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

@interface Action : NSObject {

float prixAchatAction;
float prixActuelAction;
int nbActions;

}

@property float prixAchatAction;
@property float prixActuelAction;
@property int nbActions;

  • (float)coutEnEuros;

  • (float)valeurEnEuros;

@end[/code]

action.m

[code]#import “Action.h”

@implementation Action

@synthesize prixAchatAction, prixActuelAction, nbActions;

  • (float)coutEnEuros {

    return prixAchatAction * nbActions;

    }

  • (float)valeurEnEuros {

    return prixActuelAction * nbActions;

    }

@end
[/code]

main.m

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

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

@autoreleasepool {
    
    Action *action1 = [[Action alloc] init];
    [action1 setPrixAchatAction:2.30];
    [action1 setPrixActuelAction:4.50];
    [action1 setNbActions:40];
    
    Action *action2 = [[Action alloc] init];
    [action2 setPrixAchatAction:12.19];
    [action2 setPrixActuelAction:10.56];
    [action2 setNbActions:90];
    
    Action *action3 = [[Action alloc] init];
    [action3 setPrixAchatAction:45.10];
    [action3 setPrixActuelAction:49.51];
    [action3 setNbActions:210];
    
    //creer le tableau avec nos objets
    
    NSMutableArray *tab = [NSMutableArray arrayWithObjects:action1, action2, action3, nil];
    
    
   // c'est partie pour la lecture du tableau
    for (Action *d in tab) {
        NSLog(@"le cout euros est de %.2f et la valeur en euros est de %.2f",[d coutEnEuros], [d valeurEnEuros]);
    }
    
}
return 0;

}[/code]


#8

main.m file for printing the costindollars and valueindollars for each array

NSMutableArray *stockArray = [[NSMutableArray alloc] init];

    StockHolding *s1 = [[StockHolding alloc] init];
    
    [s1 setCurrentSharePrice:20];
    [s1 setPurchaseSharePrice:13.8];
    [s1 setNumberOfShares: 1500];
    [s1 costInDollars];
    [s1 valueInDollars];
    
    StockHolding *s2 = [[StockHolding alloc] init];
    
    [s2 setCurrentSharePrice:41.99];
    [s2 setPurchaseSharePrice:127.99];
    [s2 setNumberOfShares:4599];
    [s2 costInDollars];
    [s2 valueInDollars];
    
    StockHolding *s3 = [[StockHolding alloc] init];
    [s3 setCurrentSharePrice:165.99];
    [s3 setPurchaseSharePrice:19.75];
    [s3 setNumberOfShares:243];
    [s3 costInDollars];
    [s3 valueInDollars];

    [stockArray addObject:s1];
    [stockArray addObject:s2];
    [stockArray addObject:s3];
    
    for(StockHolding *s in stockArray) {
        NSLog(@"current Price is %f, purchased at %f and %i number of shares\n ,Cost in Dollars is %f , Value in Dollars is %f ", [s currentSharePrice], [s purchaseSharePrice], [s numberOfShares],[s costInDollars],[s valueInDollars] );
    }