Hi,
Interface:
#import <Cocoa/Cocoa.h>
@interface Rectangle : NSObject
{
int width;
int height;
}
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int) width;
-(int) height;
-(int) area;
-(int) perimeter;
@end
Implementation:
#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
{
// A = H * W
return height * width;
}
-(int) perimeter
{
// P = 2(H) + 2(W)
int x = 2;
return (x * height) + (x * width);
}
@end
Test Program:
#import <Foundation/Foundation.h>
#import "Rectangle.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//CH - create an instance of a Rectangle
Rectangle *myRect = [[Rectangle alloc]init];
//CH - set the rectangle height & width
[myRect setHeight: 3];
[myRect setWidth: 4];
//CH - print out the results
NSLog(@"Height: %i",[myRect height]);
NSLog(@"Width: %i",[myRect width]);
NSLog(@"Area: %i",[myRect area]);
NSLog(@"Perimeter: %i",[myRect perimeter]);
[myRect release];
[pool drain];
return 0;
}
Output:
[Session started at 2009-09-08 14:13:10 -0400.]
2009-09-08 14:13:10.899 ch4_question7[45380:10b] Height: 3
2009-09-08 14:13:10.907 ch4_question7[45380:10b] Width: 4
2009-09-08 14:13:10.916 ch4_question7[45380:10b] Area: 12
2009-09-08 14:13:10.927 ch4_question7[45380:10b] Perimeter: 14
The Debugger has exited with status 0.
Regards,
Craig