iOS Development

Course No. 10-152-139

Chapter 18 Challenge

Do the challenge this way after you’ve read and understood chapter 19.

  1. Create a new Xcode command-line project. Name the project Chapter18Challenge.
  2. Create a class named XXXStockHolding. (Use your initials for XXX. It needs to be three characters.)
  3. Using Modern Objective-C, create the following properties.
    • an NSString named stockName
    • a float named purchaseSharePrice
    • a float named currentSharePrice
    • an int named numberOfShares
  4. Create three instance methods as follows
    • costInDollars which returns a float and has no parameters.
      • This method calculates the cost in dollars by multiplying the purchaseSharePrice by the numberOfShares. You will need to use the getter methods for these values.
    • valueInDollars which returns a float and has no parameters.
      • The method calculates the value in dollars by multiplying the currentSharePrice by the numberOfShares. You will need to use the getter methods for these values.
    • netValue which returns a float and has not parameters.
      • The method calculates the net worth by subtracting the costInDollars from the valueInDollars.
  5. Import the XXXStockHolding.h file into the main.m file.
  6. In the main.m file, inside the @autoreleaseloop statement, do the following.
    • Create three instances of the XXXStockHolding class.
    • Assign the stockName, purchaseSharePrice, currentSharePrice, numberOfShares properties by sending the setter messages to the three objects. (You can use dot notation for this.)
  7. Create an instance of NSArray with the three XXXStockHolding instances. Use the Modern Objective-C syntax to create the array.
  8. Loop through the NSArray using a “for in” loop. In the loop do the following for each object.
    • Output the stock name, cost in dollars, the value in dollars, and the net value using the appropriate instance methods.
    • Dollar amounts should be output to 2 decimal places.
    • Sample output. (NSLog timestamp omitted below. Your output will have the timestamp.)