# Challenge Help

#1

Okay ive read the chapter and the forum multiple times and I still can not produce my own solution. Can someone help me out here? Thanks

ForeignStockHolding.h

[code]//
// ForeignStockHolding.h
// Stocks
//
// Created by Madison Wilkerson on 2/9/12.
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”

@interface ForeignStockHolding : StockHolding
{
//The Instance Variable conversionRate
float conversionRate;
}

//The @property for the conversion rate AKA setter getter method

@property float conversionRate;

@end[/code]

ForeignStockHolding.m

[code]//
// ForeignStockHolding.m
// Stocks
//
// Created by Madison Wilkerson on 2/9/12.
//

#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

• (float)costInDollars;
{
float beforeConversion = [super costInDollars];
return beforeConversion * [ self conversionRate];
}

-(float) valueInDollars
{
float valuebeforeconversion = [super valueInDollars];
return valuebeforeconversion * [self conversionRate];
}

@end[/code]

StockHolding.h

[code]//
// StockHolding.h
// Stocks
//
// Created by Madison Wilkerson on 2/8/12.
//

#import <Foundation/Foundation.h>

// The class StockHoldinginherits all the instance variables
//and methods defined by the class NSObject
@interface StockHolding : NSObject
{
//It has three instance variables
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
//We are now replacing the decleration of the setter and getter methods with the @property
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

//These are the two methods for your calculation

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

StockHolding.m (I get an incomplete implementation error on @implementaion Stockholding)

[code]//
// StockHolding.m
// Stocks
//
// Created by Madison Wilkerson on 2/8/12.
//

#import “StockHolding.h”
#import “ForeignStockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

@end[/code]

main.m

[code]//
// main.m
// Stocks
//
// Created by Madison Wilkerson on 2/8/12.
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”

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

@autoreleasepool {

//Create 3 Instance of StockHolding
StockHolding *stockholding1 = [[StockHolding alloc] init];
StockHolding *stockholding2 = [[StockHolding alloc] init];
StockHolding *stockholding3 = [[StockHolding alloc] init];
ForeignStockHolding *stockholding4 = [[ForeignStockHolding alloc] init];

//Give the instance variables values for 1
[stockholding1 setPurchaseSharePrice:2.30];
[stockholding1 setCurrentSharePrice:4.50];
[stockholding1 setNumberOfShares:40];

//Give the instance variables values for 2
[stockholding2 setPurchaseSharePrice:12.19];
[stockholding2 setCurrentSharePrice:10.56];
[stockholding2 setNumberOfShares:90];

//Give the instance variables values for 3
[stockholding3 setPurchaseSharePrice:45.10];
[stockholding3 setCurrentSharePrice:49.51];
[stockholding3 setNumberOfShares:210];

//Give the instance variables for ForeignStock
[stockholding4 setPurchaseSharePrice:2.30];
[stockholding4 setCurrentSharePrice:4.50];
[stockholding4 setNumberOfShares:40];
[stockholding4 setConversionRate:0.94];

//Declare the NSMutableArray for the three sets of stocks
NSMutableArray * stocks = [[NSMutableArray alloc] init];

for(ForeignStockHolding *n in stocks)

{

//Call the methods
float cost = [n costInDollars];
float value = [n valueInDollars];
NSLog(@"Bought stock for \$%.2f, It is now at \$%.2f, I have %d shares, They cost me \$%.2f, They are now worth \$%.2f",
[n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);

}

}
return 0;

}[/code]

#2

I think this one is right. If there is anything to make it easier to understand please post.

ForeignStockHolding.h

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

@interface ForeignStockHolding : StockHolding
{
//Create an Instance Variable conversion rate
float conversionRate;
}
@property float conversionRate;

//Adding the methods to be overwritten

• (float)costInDollars;
• (float)valueInDollars;

@end[/code]

ForeignStockHolding.m

[code]#import “ForeignStockHolding.h”

@implementation ForeignStockHolding

@synthesize conversionRate;

• (float)costInDollars
{
float normalCID = [super costInDollars];
return normalCID * conversionRate;
}

• (float)valueInDollars
{
float normalVID = [super valueInDollars];
return normalVID * conversionRate;
}

@end[/code]

StockHolding.h

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

// The class StockHolding inherits all the instance variables
//and methods defined by the class NSObject
@interface StockHolding : NSObject
{
//It has three instance variables
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
//We are now replacing the decleration of the setter and getter methods with the @property
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

//These are the two methods for your calculation

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

StockHolding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

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

main.m

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

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

@autoreleasepool {

//Create 3 Instance of StockHolding
StockHolding *stockholding1 = [[StockHolding alloc] init];
StockHolding *stockholding2 = [[StockHolding alloc] init];
StockHolding *stockholding3 = [[StockHolding alloc] init];
ForeignStockHolding *stockholding4 = [[ForeignStockHolding alloc] init];

//Give the instance variables values for 1
[stockholding1 setPurchaseSharePrice:2.30];
[stockholding1 setCurrentSharePrice:4.50];
[stockholding1 setNumberOfShares:40];

//Give the instance variables values for 2
[stockholding2 setPurchaseSharePrice:12.19];
[stockholding2 setCurrentSharePrice:10.56];
[stockholding2 setNumberOfShares:90];

//Give the instance variables values for 3
[stockholding3 setPurchaseSharePrice:45.10];
[stockholding3 setCurrentSharePrice:49.51];
[stockholding3 setNumberOfShares:210];

//Give the instance variables for foreign
[stockholding4 setPurchaseSharePrice:45.10];
[stockholding4 setCurrentSharePrice:49.51];
[stockholding4 setNumberOfShares:210];
[stockholding4 setConversionRate:0.94];

//Declare the NSMutableArray for the three sets of stocks
NSMutableArray * stocks = [[NSMutableArray alloc] init];

for(StockHolding *n in stocks)

{

//Call the methods
float cost = [n costInDollars];
float value = [n valueInDollars];
NSLog(@"Bought stock for \$%.2f, It is now at \$%.2f, I have %d shares, They cost me \$%.2f, Now they are worth \$%.2f",
[n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);

}

}
return 0;

}[/code]

#3

That looks like it’s correct. It worked, right?

FYI, in ForeignStockHolding.h you don’t need to add:

[code] //Adding the methods to be overwritten

• (float)costInDollars;
• (float)valueInDollars;[/code]

Those methods are already declared in StockHolding, which this ForeignStockHolding class is based on.

#4

Oh ok that makes more sense now, thanks.