I just started to learn Objective C with Programming in Objective C by Stephen G. Kochanand and I would love to get your feedback to see if I'm getting OOP concept with Objective-C right.
GraphicObject.h
#import <Foundation/Foundation.h>
@interface GraphicObject : NSObject
{
int fillColor;
int lineColor;
BOOL filled;
}
-(void) fillColor: (int) fc;
-(void) lineColor: (int) lc;
-(int) getFilledColor;
-(int) getLineColor;
-(BOOL) filled;
@end
GraphicObject.m
#import "GraphicObject.h"
@implementation GraphicObject
//--------------------------Get and Set filled/line colors----------------------------------//
-(void) fillColor:(int)fc
{
fillColor = fc;
}
-(void) lineColor:(int)lc
{
lineColor = lc;
}
-(int) getFilledColor
{
return fillColor;
}
-(int) getLineColor
{
return lineColor;
}
//-------------------------------------Is filled?--------------------------------------------//
-(BOOL) filled
{
filled = fillColor;
return filled;
}
@end
Rectangle.h
#import "GraphicObject.h"
#import "XYPoint.h"
@interface Rectangle : GraphicObject
@property float width, height, tx, ty;
-(XYPoint *) origin;
-(void) setWidth: (float) w andHeight: (float) h;
-(void) setOrigin:(XYPoint *)pt;
-(void) translate:(XYPoint *)point;
-(float) area;
-(float) perimeter;
@end
Rectangle.m
#import "Rectangle.h"
#import "XYPoint.h"
@implementation Rectangle
{
XYPoint *origin;
}
@synthesize width, height, tx, ty;
-(void) setWidth: (float) w andHeight: (float) h
{
width = w;
height = h;
}
-(void) translate:(XYPoint *)point
{
tx = origin.x + point.x;
ty = origin.y + point.y;
}
-(void)setOrigin:(XYPoint *)pt
{
if (!origin)
origin = [[XYPoint alloc] init];
origin.x = pt.x;
origin.y = pt.y;
}
-(XYPoint *) origin
{
return origin;
}
-(float) area
{
return width * height;
}
-(float) perimeter
{
return (width + height) * 2;
}
@end
XYPoint.h
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
@property float x, y;
-(void) setX:(float)xP andY: (float)yP;
@end
XYPoint.m
#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;
-(void) setX:(float)xP andY:(float)yP
{
x = xP;
y = yP;
}
@end
Circle.h
#import "GraphicObject.h"
@interface Circle : GraphicObject
@property float radius, diameter;
-(float) getArea;
-(float) getCircumference;
-(void) setDiameter:(float)d andRadius: (float)r;
@end
Circle.m
#import "Circle.h"
@implementation Circle
@synthesize radius, diameter;
-(float) getArea
{
return (radius * radius) * 3.14;
}
-(float) getCircumference
{
return (3.14 * diameter);
}
-(void) setDiameter:(float)d andRadius: (float)r;
{
diameter = d;
radius = r;
}
@end
Triangle.h
#import "GraphicObject.h"
@interface Triangle : GraphicObject
@property float sideA, sideB, sideC, base, height;
-(float) getArea;
-(float) getPerimeter;
-(void) setBase:(float)b andHeight:(float)h;
-(void) setSideA:(float)a andB: (float)b andC: (float)c;
@end
Triangle.m
#import "Triangle.h"
@implementation Triangle
@synthesize sideA, sideB, sideC, base, height;
-(float) getArea
{
return (base/2) * height;
}
-(float) getPerimeter
{
return sideA + sideB + sideC;
}
-(void) setBase:(float)b andHeight:(float)h;
{
base = b;
height = h;
}
-(void) setSideA:(float)a andB: (float)b andC: (float)c
{
sideA = a;
sideB = b;
sideC = c;
}
@end
main
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Circle.h"
#import "Triangle.h"
#import "GraphicObject.h"
#import "XYPoint.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//----------------------------------Creating instance objects of all classes-------------------------------
Rectangle *myRectangle = [[Rectangle alloc] init];
Circle *myCircle = [[Circle alloc] init];
Triangle *myTriangle = [[Triangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];
//----------------------------------Testing XYPoint class and Rectangle class-------------------------------
[myPoint setX:5 andY:10];
[myRectangle setOrigin:myPoint];
[myRectangle setWidth:10 andHeight:7];
NSLog(@"Rectangle Width = %.1f Height = %.1f is at (%.1f,%.1f)", myRectangle.width, myRectangle.height, myRectangle.origin.x, myRectangle.origin.y);
//------------------------------------------Testing Circle class--------------------------------------------
[myCircle setDiameter:6 andRadius:3];
NSLog(@"Circle area = %.1f Circumference = %.1f", [myCircle getArea], [myCircle getCircumference]);
//-----------------------------------------Testing Triangle class-------------------------------------------
[myTriangle setSideA:5 andB:6 andC:7];
[myTriangle setBase:5 andHeight:12];
NSLog(@"Triangle Perimeter = %.1f Area = %.1f", [myTriangle getPerimeter], [myTriangle getArea]);
}
return 0;
}