// Rectangles.m Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-05-31 File created. // 2013-06-09 Last modification. // This program contains a definition of a simple class // named Rectangle. // Rectangle objects are printed rectangles in a terminal window. // It is possible to specify a width and height and a filling // character for each Rectangle object. // This program can be compiled and run in a MacOS X terminal window // with the commands // gcc Rectangles.m -o a.out -framework Foundation // ./a.out #include #import @interface Rectangle : NSObject { int rectangle_width ; int rectangle_height ; char filling_character ; } - (void) initialize_width: (int) given_rectangle_width and_height: (int) given_rectangle_height and_filler: (char) given_filling_character ; - (void) print_rectangle ; @end @implementation Rectangle - (void) initialize_width: (int) given_rectangle_width and_height: (int) given_rectangle_height and_filler: (char) given_filling_character { rectangle_width = given_rectangle_width ; rectangle_height = given_rectangle_height ; filling_character = given_filling_character ; } - (void) print_rectangle { int number_of_rows_printed = 0 ; while ( number_of_rows_printed < rectangle_height ) { printf( "\n " ) ; int number_of_characters_printed = 0 ; while ( number_of_characters_printed < rectangle_width ) { printf( "%c", filling_character ) ; number_of_characters_printed ++ ; } number_of_rows_printed ++ ; } printf( "\n" ) ; } @end int main( void ) { Rectangle* first_rectangle = [ Rectangle alloc ] ; [ first_rectangle initialize_width: 7 and_height: 4 and_filler: 'Z' ] ; [ first_rectangle print_rectangle ] ; [ first_rectangle release ] ; Rectangle* second_rectangle = [ Rectangle alloc ] ; [ second_rectangle initialize_width: 12 and_height: 3 and_filler: 'X' ] ; [ second_rectangle print_rectangle ] ; [ second_rectangle release ] ; return 0 ; } /* NOTES This is one of my first attempts to create an Objective-C program. The following page was very useful to learn to compile with command line commands http://blog.lyxite.com/2008/01/compile-objective-c-programs-using-gcc.html The following page contains discussion about writing and calling instance methods in O'C http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-4/ */