Hi!
I'm pretty new to programming and I'm working through Programming in Objective-C by Stephen Kochan.
I've got a problem I can't seem to figure out.
Its an exercise in the book where we're supposed to create a program to work with rectangles. I've included the details below.
The problem is mainly in "Rectangle.m"
I don't understand why i can't seem to use the method "intersect" to set
and
I think its related to how I defined "setOrigin" but I don't understand how it all comes together.
I noticed that I can't simply use
and
and must instead use
Code:
[origin setX: value andY: value]
.
I think they're related but I'm not sure. Thanks in advance!
this is the file XYPoint.h
Code:
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
{
float x;
float y;
}
@property float x, y;
-(void) setX: (float) xVal andY: (float) yVal;
@end
this is the file XYPoint.m
Code:
#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX: (float) xVal andY: (float) yVal;
{
x = xVal;
y = yVal;
}
@end
this is the file Rectangle.h
Code:
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle : GraphicObject
{
float width;
float height;
XYPoint *origin;
}
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) set: (float) w : (float) h;
-(float) area;
-(float) perimeter;
-(void) translate: (XYPoint *) trans;
-(Rectangle *) intersect: (Rectangle *) second;
@end
this is the file Rectangle.m
Code:
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) set: (float) w : (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;
}
-(void) translate: (XYPoint *) trans
{
origin.x += trans.x;
origin.y += trans.y;
}
-(Rectangle *) intersect: (Rectangle *) second
{
Rectangle * new = [[Rectangle alloc] init];
new.width = (origin.x + width) - second.origin.x;
new.height = (second.origin.y + second.height) - origin.y;
new.origin.x = 4;
new.origin.y = 5;
return new;
}