Two Solutions


#1

So I have two solutions to this challenge. I wasn’t really sure what I was supposed to print,
so I chose to print the costInDollars and valueInDollars.

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; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares

@end[/code]

StockHolding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize currentSharePrice;
@synthesize purchaseSharePrice;
@synthesize numberOfShares;

  • (float)costInDollars
    {
    return purchaseSharePrice * numberOfShares;
    }

  • (float)valueInDollars
    {
    return currentSharePrice * numberOfShares;
    }

@end
[/code]

main.m (first)

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

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

@autoreleasepool {
    
    StockHolding *firstStock = [[StockHolding alloc] init];
    StockHolding *secondStock = [[StockHolding alloc] init];
    StockHolding *thirdStock = [[StockHolding alloc] init];
    
    [firstStock setPurchaseSharePrice:2.30];
    [firstStock setCurrentSharePrice:4.50];
    [firstStock setNumberOfShares:40];
    
    [secondStock setPurchaseSharePrice:12.19];
    [secondStock setCurrentSharePrice:10.56];
    [secondStock setNumberOfShares:90];
    
    [thirdStock setPurchaseSharePrice:45.10];
    [thirdStock setCurrentSharePrice:49.51];
    [thirdStock setNumberOfShares:250];
    
    NSMutableArray *stocks = [[NSMutableArray alloc] initWithObjects:firstStock, secondStock, thirdStock, nil];
    
    for (int i = 0; i < [stocks count]; i++) {
        NSLog(@"The shares costed %.2f and valued at %.2f",
              [[stocks objectAtIndex:i] costInDollars], [[stocks objectAtIndex:i] valueInDollars]);
    }
    
}
return 0;

}[/code]

I chose the initialization for-loop again, because the

for (StockHolding *s in stocks) { statements }
didn’t really click for me and I first discovered after reading others submission. I tried with NSArray and NSString, but didn’t think of StockHolding as class.

So the other main.m solution was

for (StockHolding *s in stocks) { NSLog(@"Share: %.2f. Value: %.2f.", [s costInDollars], [s valueInDollars]); }

Is it possible to make the code more effective? I feel like it is unnecessary to write 9 lines of code to assign the three values for the three stocks.
I also feel like it should be possible to alloc and init three pointers at the same time, instead of

StockHolding *firstStock = [[StockHolding alloc] init]; StockHolding *secondStock = [[StockHolding alloc] init]; StockHolding *thirdStock = [[StockHolding alloc] init];


#2

[quote=“Stalkr”]

Is it possible to make the code more effective? I feel like it is unnecessary to write 9 lines of code to assign the three values for the three stocks.
I also feel like it should be possible to alloc and init three pointers at the same time, instead of

StockHolding *firstStock = [[StockHolding alloc] init]; StockHolding *secondStock = [[StockHolding alloc] init]; StockHolding *thirdStock = [[StockHolding alloc] init];[/quote]

Same question.
I’d also like to know.


#3

You don’t really need three variables. Reuse the pointer by making it point to a new object:

[code]
NSMutableArray *stocks = [[NSMutableArray alloc] init];
StockHolding *stock;

    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:2.30];
    [stock setCurrentSharePrice:4.50];
    [stock setNumberOfShares:40];
    [stocks addObject:stock];

    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:12.19];
    [stock setCurrentSharePrice:10.56];
    [stock setNumberOfShares:90];
    [stocks addObject:stock];


    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:45.10];
    [stock setCurrentSharePrice:49.51];
    [stock setNumberOfShares:250];
    [stocks addObject:stock];[/code]

#4

[quote=“AaronHillegass”]You don’t really need three variables. Reuse the pointer by making it point to a new object:

[code]
NSMutableArray *stocks = [[NSMutableArray alloc] init];
StockHolding *stock;

    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:2.30];
    [stock setCurrentSharePrice:4.50];
    [stock setNumberOfShares:40];
    [stocks addObject:stock];

    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:12.19];
    [stock setCurrentSharePrice:10.56];
    [stock setNumberOfShares:90];
    [stocks addObject:stock];


    stock = [[StockHolding alloc] init];
    [stock setPurchaseSharePrice:45.10];
    [stock setCurrentSharePrice:49.51];
    [stock setNumberOfShares:250];
    [stocks addObject:stock];[/code][/quote]

Wouldn’t that make an NSMutableArray with [“stock”, “stock”, “stock”]?
I just made three pointers because I thought the array would be [“firstStock”, “secondStock”, “thirdStock”].

I am used to Python, please tell if it’s a lot different in Objective-C.


#5

‘stock’ is just a variable that holds an address. When we addObject:stock, we are copying that address into the array. So, each time we call addObject: we are putting a different address in.

So the array will hold three different objects.


#6

[quote=“AaronHillegass”]‘stock’ is just a variable that holds an address. When we addObject:stock, we are copying that address into the array. So, each time we call addObject: we are putting a different address in.

So the array will hold three different objects.[/quote]

So the array would be more something like [“0x7fff5fbff7fa”, “0x7fff5fbff7fb”, “0x7fff5fbff7fc”]?

where i is just a test integer, not used in this solution, but just to try to understand what the array is holding.

So the for loop asks “what exist at the first address in this array?”, then second, third etc? I hope my description makes sense.