Need help on challenge


#1

This is what i have so far:

.h file

[code]#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
    [/code]

My .m file

[code]#import “StockHolding.h”

@implementation StockHolding
@synthesize purchaseSharePrice;
@synthesize currentSharePrice;
@synthesize numberofShares;

  • (float)costInDollars;
    {
    return purchaseSharePrice * numberOfShares;
    }
  • (float)valueInDollars;
    {
    return currentSharePrice * numberOfShares;
    }
    @end
    [/code]

My main file:

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

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

@autoreleasepool {
    
    NSMutableArray *holdings=[[NSMutableArray alloc] init];
    
    
    StockHolding *stockA = [[StockHolding alloc] init];
    StockHolding *stockB = [[StockHolding alloc] init];        
                                                                
    [stockA setPurchaseSharePrice: 10];
    [stockA setCurrentSharePrice: 15];
    [stockA setNumberofShares: 10];
                                                                
                                                                
                                                                
    [stockB setPurchaseSharePrice: 5];
    [stockB setCurrentSharePrice: 10];
    [stockB setNumberofShares: 10];
                                                                
                                                                
[holdings addObject:stockA];
[holdings addObject:stockB];
                                                
    for (StockHolding *d in holdings)
    {
    float cost=[d costInDollars];
    float currentvalue=[d valueInDollars];
                                                                    
    NSLog(@"cost is %f, currentvalue is %f",cost,currentvalue);
                                                                }
                                                                
                                                                
    
   
}
return 0;

}

[/code]

This is the result I’m getting:

2013-09-16 16:48:43.246 Stocks[5930:303] cost is 0.000000, currentvalue is 0.000000 2013-09-16 16:48:43.248 Stocks[5930:303] cost is 0.000000, currentvalue is 0.000000

Why am I getting values of 0.000000 in my solution?

Thanks in advance.


#2

There is a spelling error, actually a case error, in one of the instance variables.

[quote] @interface StockHolding : NSObject { float purchaseSharePrice; float currentSharePrice; int numberOfShares; // <--- This should be numberofShares } [/quote]


#3

[quote=“ibex10”]There is a spelling error, actually a case error, in one of the instance variables.

[quote] @interface StockHolding : NSObject { float purchaseSharePrice; float currentSharePrice; int numberOfShares; // <--- This should be numberofShares } [/quote][/quote]

:blush: haah man sorry missed that! now i feel so dumb…

THANKS!