I simply changed the integer data type,
int, to a
float data type in order to accommodate floating-point values. I then changed the coordinates to numbers that might reflect those of a higher-resolution device.
Console output:
[Session started at 2009-07-13 14:41:44 +0200.]
2009-07-13 14:41:44.397 Prog ex8.2[315:10b] Rectangle: w = 5.000000, h = 8.000000
2009-07-13 14:41:44.400 Prog ex8.2[315:10b] Origin at (100.449997, 198.800003)
2009-07-13 14:41:44.400 Prog ex8.2[315:10b] Area = 40.000000, Perimeter = 26.000000
The Debugger has exited with status 0.
main
#import "Rectangle.h"
#import "XYPoint.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRect = [[Rectangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];
[myPoint setX: 100.45 andY: 198.80];
[myRect setWidth: 5 andHeight: 8];
myRect.origin = myPoint; // [myRect setOrigin: myPoint];
NSLog (@"Rectangle: w = %f, h = %f", myRect.width, myRect.height); // equal to [myRect width], [myRect height]
NSLog (@"Origin at (%f, %f)", myRect.origin.x, myRect.origin.y);
NSLog (@"Area = %f, Perimeter = %f", [myRect area], [myRect perimeter]);
[[myRect origin] release];
[myRect release];
[myPoint release];
[pool drain];
return 0;
}
Rectangle.h
//
// Rectangle.h
// Prog ex8.2
//
// Created by Neil Hohmann on 7/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
}
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(float) area;
-(float) perimeter;
@end
Rectangle.m
//
// Rectangle.m
// Prog ex8.2
//
// Created by Neil Hohmann on 7/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) setWidth: (float) w andHeight: (float) h
{
width = w;
height = h;
}
-(void) setOrigin: (XYPoint *) pt
{
origin = [[XYPoint alloc] init];
[origin setX: pt.x andY: pt.y];
}
-(float) area
{
return width * height;
}
-(float) perimeter
{
return (width + height) * 2;
}
-(XYPoint *) origin
{
return origin;
}
@end
XYPoint.h
//
// Rectangle.h
// Prog ex8.2
//
// Created by Neil Hohmann on 7/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
}
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(float) area;
-(float) perimeter;
@end
XYPoint.m
//
// XYPoint.m
// Prog ex8.2
//
// Created by Neil Hohmann on 7/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX: (float) xVal andY: (float) yVal
{
x = xVal;
y = yVal;
}
@end