Help On Challenge


#1

stockholding.h

[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]

stockholding.m

[code]#import “StockHolding.h”

@implementation StockHolding
@synthesize purchaseSharePrice;
@synthesize currentSharePrice;
@synthesize numberOfShares;

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


foreignstockholding.h

[code]#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding

{
float conversionRate;
}
@property float conversionRate;

@end
[/code]

foreignstockholding.m

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding
@synthesize conversionRate;

  • (float)costInDollars
    {
    return [super costInDollars]*[self conversionRate];
    }
  • (float)valueInDollars
    {
    return [super valueInDollars]*[self conversionRate];
    }
    @end
    [/code]

main.m

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

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

@autoreleasepool {
    
    NSMutableArray *holdings=[[NSMutableArray alloc] init];
    

    
    ForeignStockHolding *stockA = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *stockB = [[ForeignStockHolding alloc] init];


    
    [stockA setPurchaseSharePrice: 10];
    [stockA setCurrentSharePrice: 15];
    [stockA setNumberOfShares: 100];
    [stockA setConversionRate:.5];
                                                                
                                                                
                                                                
    [stockB setPurchaseSharePrice: 5];
    [stockB setCurrentSharePrice: 10];
    [stockB setNumberOfShares: 100];
    [stockB setConversionRate:.9];
    
    [holdings addObject:stockA];
    [holdings addObject:stockB];

                                                
    for (ForeignStockHolding *d in holdings)
    {

                                                                    
        NSLog(@"cost %f, value %f"), [d costInDollars], [d valueInDollars];
        
   
}
return 0;

}
}
[/code]

this is the result i am getting:

2013-10-08 15:44:12.351 Stocks[1675:303] cost 0.000000, value 0.000000 2013-10-08 15:44:12.353 Stocks[1675:303] cost 0.000000, value 0.000000 Program ended with exit code: 0

why are all the values 0?

I’m also getting the warning message "More “%” conversions than data arguments.
:question:


#2

sorry, i figured it out…misplaced the parenthesis on NSLog.


#3

There is a coding anomaly.

Change this:

for (ForeignStockHolding *d in holdings) { NSLog(@"cost %f, value %f"), [d costInDollars], [d valueInDollars]; // <--- anomaly on this line. } To:

        for (ForeignStockHolding *d in holdings)
        {                                                          
            NSLog(@"cost %f, value %f", [d costInDollars], [d valueInDollars]);
        }

#4

yep, changed it.

thanks ibex.


#5

The compiler should have flagged this with an error or warning.

Resolve all errors and warnings (fix or let the compiler know you know (by casting etc).

Check and resolve every yellow triangle and red octagon.