// Olympics.m Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-06-16 File created. // 2013-06-16 Last modification. /* This program demonstrates: - a simple Objective-C class - the use of the standard NSArray class - initializing an NSArray with objects In this program the end of an array is marked with an unusable object. I know that this is not the best approach to discover when the end of an array is found. It is a nice exercise to modify the program so that the unusable object is not needed. */ #include #import #import "useful_constants_oc.h" // import a header file in this folder @interface Olympics : NSObject { int olympic_year ; NSString* olympic_city ; NSString* olympic_country ; } - (id) init_year: (int) given_olympic_year and_city: (NSString*) given_olympic_city and_country: (NSString*) given_olympic_country ; - (int) get_year ; // a getter method - (void) print_olympics_data ; @end @implementation Olympics - (id) init_year: (int) given_olympic_year and_city: (NSString*) given_olympic_city and_country: (NSString*) given_olympic_country { self = [ super init ] ; if ( self ) { olympic_year = given_olympic_year ; olympic_city = [ [ NSString alloc ] initWithString: given_olympic_city ] ; olympic_country = [ [ NSString alloc ] initWithString: given_olympic_country ] ; } return self ; } - (int) get_year { return olympic_year ; } - (void) print_olympics_data { NSString* text_to_print = [ NSString stringWithFormat: @"\n In %d, Olympic Games were held in %@, %@.\n", olympic_year, olympic_city, olympic_country ] ; // The following statement converts a NSString to a // C-style string. According to documentation, the memory // of the C-style string will be automatically freed. printf( "%s", [ text_to_print UTF8String ] ) ; } @end int main( void ) { NSAutoreleasePool* autorelease_pool = [ [ NSAutoreleasePool alloc ] init ] ; // Next we create an array by using the standard NSArray class. // The array will contain the Olympics objects that are created // inside the long statement. NSArray* olympics_table = [ NSArray arrayWithObjects: [ [ Olympics alloc ] init_year: 1896 and_city: @"Athens" and_country: @"Greece" ], [ [ Olympics alloc ] init_year: 1900 and_city: @"Paris" and_country: @"France" ], [ [ Olympics alloc ] init_year: 1904 and_city: @"St. Louis" and_country: @"U.S.A" ], [ [ Olympics alloc ] init_year: 1906 and_city: @"Athens" and_country: @"Greece" ], [ [ Olympics alloc ] init_year: 1908 and_city: @"London" and_country: @"Great Britain" ], [ [ Olympics alloc ] init_year: 1912 and_city: @"Stockholm" and_country: @"Sweden" ], [ [ Olympics alloc ] init_year: 1920 and_city: @"Antwerp" and_country: @"Belgium" ], [ [ Olympics alloc ] init_year: 1924 and_city: @"Paris" and_country: @"France" ], [ [ Olympics alloc ] init_year: 1928 and_city: @"Amsterdam" and_country: @"Netherlands" ], [ [ Olympics alloc ] init_year: 1932 and_city: @"Los Angeles" and_country: @"U.S.A." ], [ [ Olympics alloc ] init_year: 1936 and_city: @"Berlin" and_country: @"Germany" ], [ [ Olympics alloc ] init_year: 1948 and_city: @"London" and_country: @"Great Britain" ], [ [ Olympics alloc ] init_year: 1952 and_city: @"Helsinki" and_country: @"Finland" ], [ [ Olympics alloc ] init_year: 1956 and_city: @"Melbourne" and_country: @"Australia" ], [ [ Olympics alloc ] init_year: 1960 and_city: @"Rome" and_country: @"Italy" ], [ [ Olympics alloc ] init_year: 1964 and_city: @"Tokyo" and_country: @"Japan" ], [ [ Olympics alloc ] init_year: 1968 and_city: @"Mexico City" and_country: @"Mexico" ], [ [ Olympics alloc ] init_year: 1972 and_city: @"Munich" and_country: @"West Germany" ], [ [ Olympics alloc ] init_year: 1976 and_city: @"Montreal" and_country: @"Canada" ], [ [ Olympics alloc ] init_year: 1980 and_city: @"Moscow" and_country: @"Soviet Union" ], [ [ Olympics alloc ] init_year: 1984 and_city: @"Los Angeles" and_country: @"U.S.A." ], [ [ Olympics alloc ] init_year: 1988 and_city: @"Seoul" and_country: @"South Korea" ], [ [ Olympics alloc ] init_year: 1992 and_city: @"Barcelona" and_country: @"Spain" ], [ [ Olympics alloc ] init_year: 1996 and_city: @"Atlanta" and_country: @"U.S.A." ], [ [ Olympics alloc ] init_year: 2000 and_city: @"Sydney" and_country: @"Australia" ], [ [ Olympics alloc ] init_year: 2004 and_city: @"Athens" and_country: @"Greece" ], [ [ Olympics alloc ] init_year: 2008 and_city: @"Beijing" and_country: @"China" ], [ [ Olympics alloc ] init_year: 2012 and_city: @"London" and_country: @"Great Britain" ], [ [ Olympics alloc ] init_year: 2016 and_city: @"Rio de Janeiro" and_country: @"Brazil" ], [ [ Olympics alloc ] init_year: 9999 and_city: @"end of" and_country: @"data" ], nil ] ; int given_year ; printf( "\n This program can tell you where the summer Olympic " "\n Games were held in a given year. Please, give a year " "\n using four digits: " ) ; scanf( "%d", &given_year ) ; int olympics_index = 0 ; int table_search_status = SEARCH_NOT_READY ; while ( table_search_status == SEARCH_NOT_READY ) { Olympics* olympics_in_table = [ olympics_table objectAtIndex: olympics_index ] ; if ( [ olympics_in_table get_year ] == given_year ) { [ olympics_in_table print_olympics_data ] ; table_search_status = SEARCH_IS_READY ; } else if ( [ olympics_in_table get_year ] == 9999 ) { printf( "\n\n Sorry, no Olympic Games were held in %d.\n", given_year ) ; table_search_status = SEARCH_IS_READY ; } else { olympics_index ++ ; } } [ autorelease_pool drain ] ; } /* NOTES: This program can be compiled and run with commands such as: gcc Olympics.m -o runnablefile.out -framework Foundation ./runnablefile.out The above command works in a MacOS X Terminal window provided that the necessary tools are installed. In MacOS X terminal window, as well as terminal windows or command prompt windows in other operating systems, the commands can be easily repeated by using the Arrow Up and Arrow Down keys. */