Skip to content

Instantly share code, notes, and snippets.

@goldshtn
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldshtn/3d80f66da5188e2e5e66 to your computer and use it in GitHub Desktop.
Save goldshtn/3d80f66da5188e2e5e66 to your computer and use it in GitHub Desktop.
iOS Lab 1

In this lab, you will experiment with Objective C fundamentals in a Mac terminal application that does not display any user interface. Everything you do in this lab is applicable to iOS as well – we will not use any API that is not available in iOS.

Begin by creating an Xcode project of type OS X > Application > Command Line Tool. (In the Type combo box, select “Foundation” if it isn’t selected.) Add classes as necessary per the instructions below using File > New > File, OS X > Cocoa > Objective C Class.

Implement a PlayingCard class that has the following methods, initializers, and properties:

  • NSString* suit property
  • NSUInteger rank property (valid values are 1-13, where 1 = “Ace”, …, 13 = “King”)
  • NSString* contents read-only property that returns the card name (e.g. “Jack of Spades”)
  • (NSArray *)validSuits class method that returns an array of valid suits (“Diamonds”, “Hearts”, “Spades”, “Clubs”)
  • (NSArray *)validRanks method that returns an array of valid ranks (1-13) – you might need the [NSNumber numberWithUnsignedInteger:] class method here, or use the @ literal syntax
  • Initializer: (id)initWithRank:(NSUInteger)rank suit:(NSString *)suit which checks that the rank and suit are valid

Implement a Deck class that has the following methods:

  • (void)addCard:(PlayingCard *)card – stores the card in an NSMutableArray
  • (PlayingCard *)drawRandomCard – draws a random card from the array; use the arc4random() method to obtain a random number. Remove the card before returning it.

Implement a PlayingDeck class that derives from Deck.

  • In its init method, add all 52 playing cards to the deck.
  • Make sure you use the [Card validSuits] and [Card validRanks] methods.

Test your code by creating a PlayingDeck instance in the main.m file, drawing a few random cards, and using the NSLog function to print out the cards’ contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment