Here is my solution for Page 74 - Excercise 7 (Chapter 4). Appears to work.
The Implementation File:
#import "Rectangle.h"
@implementation Rectangle
- (void) setWidth: (int) w {
width = w;
}
- (void) setHeight: (int) h {
height = h;
}
- (int) width{
return width;
}
- (int) height{
return height;
}
- (int) area {
return width * height;
}
- (int) perimeter {
return (width + height) * 2;
}
@end
And the Main File:
#import <Foundation/Foundation.h>
#import "Rectangle.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRectangle;
myRectangle = [[Rectangle alloc] init];
[myRectangle setWidth: 5 ];
[myRectangle setHeight: 6 ];
NSLog(@"The rectangle is %i wide by %i high", [myRectangle width], [myRectangle height]);
NSLog(@"Perimeter is %i and the area contained in the rectangle is %i.", [myRectangle perimeter], [myRectangle area]);
[myRectangle release];
[pool drain];
return 0;
}