Reputation: 402
I am the end of my chapter and it has asked me to do the following:
Create new Foundation Command Line Tool called Stocks. Then create a class called StockHolding to represent a stock that you have purchased. It will be a subclass of NSObject. For instance variables, it will have two floats named purchaseSharePrice and currentSharePrice and one int named numberOfShares. Create accessor methods for the instance variables. Create two other instance methods:
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares In main(), fill an array with three instances of StockHolding. Then iterate through the array printing out the value of each.
So as such I followed what I had learned through out the chapter. I created my terminal app (which is only printing to the console like it should) and I added an Objective-C class which I then adjusted with what I thought to be the proper information.
For my .h file I filled it with the following:
@interface StocksHolding : NSObject
//Declare instance variables {
float purchaseSharePrices;
float currentSharePrices;
int numberOfShares; }
//Decalre methods
-(float) costOfDollars; // purchaseSharePrice*numberOfShares
-(float) valueInDollars; // currentSharePrices*numberOfShares
//Declare setter and getter methods
@property float purchaseSharePrices;
@property float currentSharePrices;
@property int numberOfShares;
@end
So great, I did that, no issues as far as I could tell. I proceeded to write out my implementation in my .m file. Here is what I wrote:
@implementation StocksHolding
@synthesize purchaseSharePrices, currentSharePrices, numberOfShares;
-(float) coastOfDollars{
return (purchaseSharePrices * numberOfShares);
}
-(float) valueInDollars{
return (currentSharePrices * numberOfShares);
}
@end
Now, before I move on I would like to note that I am getting a "yellow triangle with an '!' in it" (sorry I can't remember the name off hand). When I click on it is says "Incomplete implementation. I went to find what that means by looking in the side bar and it says "Method definition 'costOfDollars' not found." That confuses me because I am almost sure I defined it. So at this point, I just wanted to move on before dealing with that, just to see if my program ran some what correctly. I went to my main.m file and add the following code:
#import <Foundation/Foundation.h>
#import "StocksHolding.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
StocksHolding *myFirstStock;
StocksHolding *mySecondStock;
StocksHolding *myThirdStock;
NSArray *myStockHoldings = [NSArray arrayWithObjects:myFirstStock,mySecondStock,myThirdStock, nil];
//Set the values for myFirstStock
[myFirstStock setNumberOfShares:(3)];
[myFirstStock setPurchaseSharePrices:(10.50)];
[myFirstStock setCurrentSharePrices:(30.86)];
//Set the values for mySecondStock
[mySecondStock setNumberOfShares:(4)];
[mySecondStock setPurchaseSharePrices:(40.80)];
[mySecondStock setCurrentSharePrices:(30.96)];
//Set the values for myThirdStock
[myThirdStock setNumberOfShares:(20)];
[myThirdStock setPurchaseSharePrices:(90.50)];
[myThirdStock setCurrentSharePrices:(108.93)];
//Printing the information that has been gathered
NSLog(@"There are %lu stocks which I own", [myStockHoldings count]);
NSLog(@"The number of shares I own in my first stock is %d", [myFirstStock numberOfShares] );
NSLog(@"I bought the first stock for a price of %f and the current price is %f", [myFirstStock purchaseSharePrices], [myFirstStock costOfDollars]);
NSLog(@"The value of my stock now is %f", [myFirstStock valueInDollars]);
}
return 0;
}
After that I was pretty impressed with what I had accomplished in terms of figuring this all out in my head, but here is where it all blew up. I ran the program, it ran fine, but my output was not what I was expecting:
[Switching to process 3063 thread 0x0] 2012-01-12 15:12:52.389 Stocks[3063:707] There are 0 stocks which I own 2012-01-12 15:12:52.392 Stocks[3063:707] The number of shares I own in my first stock is 0 2012-01-12 15:12:52.394 Stocks[3063:707] I bought the first stock for a price of 0.000000 and the current price is 0.000000 2012-01-12 15:12:52.395 Stocks[3063:707] The value of my stock now is 0.000000 Program ended with exit code: 0
All of my values are zero. I don't understand why this is, which probably means I am not understanding something I was suppose to in this chapter (the chapter was about creating my own class). Could someone help me understand what exactly I am doing incorrectly here?
Upvotes: 0
Views: 804
Reputation: 1
For those of us still in search of an answer (or at least one possible way of doing it), here's my attempt. Be gentle with me as I'm a complete newbie in Obj. C (or programming in general, really), though it seems to work well for me. Note the code listed is based on the question posed in Hillegass' book:
main.m:
// main.m
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "StockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
StockHolding *firstInstance = [[StockHolding alloc] init];
StockHolding *secondInstance = [[StockHolding alloc] init];
StockHolding *thirdInstance = [[StockHolding alloc] init];
// Create an empty mutable array
NSMutableArray *stockArray = [NSMutableArray array];
// Add instances
[stockArray addObject:firstInstance];
[stockArray addObject:secondInstance];
[stockArray addObject:thirdInstance];
[firstInstance setPurchaseSharePrice:2.3];
[firstInstance setCurrentSharePrice:4.5];
[firstInstance setNumberOfShares:40];
[secondInstance setPurchaseSharePrice:12.10];
[secondInstance setCurrentSharePrice:10.56];
[secondInstance setNumberOfShares:20];
[thirdInstance setPurchaseSharePrice:45.10];
[thirdInstance setCurrentSharePrice:48.51];
[thirdInstance setNumberOfShares:210];
for (id stock in stockArray)
{
// Calculate the (current) value of the stock using the valueInDollars method
float value = [stock valueInDollars];
// Calculate the purchase price of the stock using the costInDollars method
float cost = [stock costInDollars];
// Calculate the profit gained thus far
float profit = (value - cost);
// Return the results to the log
NSLog(@"You've got %d stocks, which costed you %.2f and are now worth %.2f. The profit for this stock thus far is %.2f", [stock numberOfShares], cost, value, profit);
}
}
return 0;
}
Here's the StockHolding.h file. Note that the accessor shortcuts @property and @synthesize are being used as asked for in the book.
//
// StockHolding.h
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
@end
And finally, StockHolding.m:
// StockHolding.m
// Stocks
//
// Created by Eelco Plugge on 7/19/13.
// Copyright (c) 2013 Eelco Plugge. All rights reserved.
//
#import "StockHolding.h"
@implementation StockHolding
@synthesize currentSharePrice, purchaseSharePrice, numberOfShares;
- (float)costInDollars
{
// Cacluate costInDollar, purchaseSharePrice * numberOfShares
return purchaseSharePrice * numberOfShares;
};
- (float)valueInDollars{
// Calcuate valueInDollars, currentSharePrice * numberOfShares
return currentSharePrice * numberOfShares;
};
@end
Hope this helps anyone currently going through the book or serve as an interesting (or possibly comical attempt at Obj. C) bit of info.
Regards, Eelco
Upvotes: 0
Reputation: 181027
The latter problem is that you declared but didn't initialize your classes in main;
StocksHolding *myFirstStock;
StocksHolding *mySecondStock;
StocksHolding *myThirdStock;
should be
StocksHolding *myFirstStock = [[StocksHolding alloc] init];
StocksHolding *mySecondStock = [[StocksHolding alloc] init];
StocksHolding *myThirdStock = [[StocksHolding alloc] init];
The first part of the code has some problems too; you probably don't want to declare your instance variables in the .h but instead in the .m file, no need to make them visible and also have property accessors with the same name. Also, there's a commented out { that I assume is a formatting problem...?
Probably not all errors (not where I can compile and test right now), but I'm sure someone more eagle eyed will correct me :)
Upvotes: 1