[ Team LiB ] Previous Section Next Section

Exercises

1:

Look up the NSCalendarDate class in your documentation. Then add a new category to NSCalendarDate called ElapsedDays. In that new category, add a method based on the following method declaration:


-(unsigned long) numberOfElapsedDays: (NSCalendarDate *) theDate;

Have the new method return the number of elapsed days between the receiver and the argument to the method. Write a test program to test your new method. (Hint: Look at the years:months:days:hours:minutes:seconds: sinceDate: method.)

2:

Modify the lookup: method developed in this chapter for the AddressBook class so that partial matches of a name can be made. The message expression


[myBook lookup: @"steve"]

should match an entry that contains the string steve anywhere within the name.

3:

Modify the lookup: method developed in this chapter for the AddressBook class to search the address book for all matches. Have the method return an array of all such matching address cards or nil if no match is made.

4:

Add new fields of your choice to the AddressCard class. Some suggestions are separating the name field into first and last name fields and adding address (perhaps with separate state, city, ZIP, and country fields) and phone number fields. Write appropriate setter and getter methods, and ensure that the fields are displayed properly by the print and list methods.

5:

After completing exercise 3, modify the lookup: method from exercise 2 to perform a search on all the fields of an address card. Can you think of a way to design your AddressCard and AddressBook classes so that the latter does not have to know all the fields stored in the former?

6:

Add the method removeName: to the AddressBook class to remove someone from the address book given this declaration for the method:


-(BOOL) removeName: (NSString *) theName;
7:

Use the lookup: method developed in exercise 2. If the name is not found, or multiple entries exist, have the method return NO. If the person is successfully removed, have it return YES.

8:

Using the Fraction class defined in Part I, "The Objective-C Language," set up an array of fractions with some arbitrary values. Then write some code that finds the sum of all the fractions stored in the array. Make sure you modify the Fraction class as appropriate to run under Foundation.

9:

Using the Fraction class defined in Part I, set up a mutable array of fractions with arbitrary values. Then sort the array using the sortUsingSelector: method from the NSMutableArray class. Add a Comparison category to the Fraction class and implement your comparison method in that category. Make sure you modify the Fraction class as appropriate to run under Foundation.

10:

Define three new classes, called Song, PlayList, and MusicCollection. A Song object will contain information about a particular song, such as its title, artist, album, and playing time. A PlayList object will contain the name of the playlist and a collection of songs, and a MusicCollection object will contain a collection of playlists, including a special master playlist called library that contains every song in the collection. Define these three classes and write methods to do the following:

  • Create a Song object and set its information.

  • Create a Playlist object and add and remove songs to and from a playlist. A new song should be added to the master playlist if it's not already there. Make sure that, if a song is removed from the master playlist, it is removed from all playlists in the music collection as well.

  • Create a MusicCollection object and add and remove playlists to and from the collection.

  • Search and display the information about any song, playlist, or the entire music collection.

Make sure all your classes do not leak memory!

11:

Write a program that takes an array of integer objects and produces a frequency chart that lists each integer and how many times it occurs in the array. Use an NSCountedSet object for constructing your frequency counts.


    [ Team LiB ] Previous Section Next Section