home *** CD-ROM | disk | FTP | other *** search
- // LAB SOLUTION: Fruit, Apple, and Banana classes
-
- // Main program for testing the Fruit, Apple, and Banana classes
-
- #import "Fruit.h"
- #import "Apple.h"
- #import "Banana.h"
-
- main ( argc, argv )
- int argc;
- char *argv[];
- {
- id aFruit, anApple, aBanana;
-
- printf ("\nCreate a Fruit instance...\n");
- aFruit = [[Fruit alloc] init]; //allocate and initialize
- printf("aFruit, address %lx\n\t The diameter is %d and color is %s.\n",
- aFruit, //address of aFruit
- [aFruit diameter], //diameter of aFruit
- [aFruit color]); //color of aFruit
-
- printf ("Grow the Fruit...\n");
- [aFruit grow];
- printf("aFruit, address %lx\n\t The diameter is %d and color is %s.\n",
- aFruit, //address of aFruit
- [aFruit diameter],
- [aFruit color]);
-
- printf ("Create an Apple instance...\n");
- anApple = [[Apple alloc] init];
- printf("anApple, address %lx\n\t The diameter is %d, color is %s, and flavor is %s.\n",
- anApple, // address of anApple
- [anApple diameter],
- [anApple color],
- [anApple flavor]);
-
- printf ("Change the apple's color, flavor, and diameter...\n");
- [[[anApple setColor: "bright red"] setFlavor: "Macintosh"] setDiameter: 7] ;
- printf("anApple, address %lx\n\t The diameter is %d, color is %s, and flavor is %s.\n",
- anApple, // address of anApple
- [anApple diameter],
- [anApple color],
- [anApple flavor]);
-
- printf ("Grow the apple...\n");
- [anApple grow];
- printf("anApple, address %lx\n\t The diameter is %d, color is %s, and flavor is %s.\n",
- anApple, // address of anApple
- [anApple diameter],
- [anApple color],
- [anApple flavor]);
-
- /* Additions to Create, Change, and Grow a Banana. */
-
- printf ("Create a Banana instance...\n");
- aBanana = [[Banana alloc] init];
- printf("aBanana, address %lx\n\t The diameter is %d, length is %d, and color is %s.\n",
- aBanana, // address of aBanana
- [aBanana diameter],
- [aBanana length],
- [aBanana color]);
-
- printf ("Change the banana's diameter, length, and color...\n");
- [[[aBanana setColor: "green"] setLength: 6] setDiameter: 3] ;
- printf("aBanana, address %lx\n\t The diameter is %d, length is %d, and color is %s.\n",
- aBanana, // address of aBanana
- [aBanana diameter],
- [aBanana length],
- [aBanana color]);
-
- printf ("Grow the banana...\n");
- [aBanana grow];
- printf("aBanana, address %lx\n\t The diameter is %d, length is %d, and color is %s.\n",
- aBanana, // address of aBanana
- [aBanana diameter],
- [aBanana length],
- [aBanana color]);
-
- } // end of program
-