Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
}
share|improve this question
    
This is primarily an issue for Stack Exchange sites, and a little less crucial when you're looking at code in your full-screen IDE. But, please try to avoid putting so much empty whitespace (empty lines) in your code. It makes it harder to read with so much scrolling required. Thanks. Otherwise, a good question :) –  Nate May 24 '13 at 21:01
add comment

1 Answer 1

up vote 1 down vote accepted

I have to make the same suggestion as for your last question: use properties and stick to name conventions

Your code as it is

#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

Your code with name conventions (setters of an ivar prefixed with set…, getters named as the ivar/property)

#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject

{
    int fillColor;
    int lineColor;
    BOOL filled;
}

-(void) setFillColor: (int) fc;
-(void) setLineColor: (int) lc;
-(int) filledColor;
-(int) lineColor;
-(BOOL) filled;

@end

Your code with properties

#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject

{
    int fillColor;
    int lineColor;
    BOOL filled;
}
@property int fillColor;
@property int lineColor;
@property BOOL filled;

@end

But actually for nowadays compilers you don't need to declare iVars for properties

#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject

@property int fillColor;
@property int lineColor;
@property BOOL filled;

@end

and you also dont need to declare synthesize, as it is default now

As properties generate setters7getters for you, the implementation could be

#import "GraphicObject.h"

@implementation GraphicObject

@end

Cocoa's name conventions suggest not to use the word and in method names to indicate the setting of an object's state

-(void) setDiameter:(float)d andRadius: (float)r;

becomes

-(void) setDiameter:(float)d radius: (float)r;

BTW: diamater = radius * 2, so there is no need to pass both

and is suggested for action that might be triggered after setting.
Some made up example

-(void) sendData:(NSData *)data 
           toURL:(NSURL *) url 
andPerformSuccessSelector:(SEL)successSelector 
   performFailureSelector:(SEL)failureSelector;

-(void) setSideA:(float)a b: (float)b c: (float)c

instead of XYPoint class

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void) setX:(float)xP andY:(float)yP
{
    x = xP;
    y = yP;
}

@end

you could use the CGPoint struct (iOS) or NSPoint (Cocoa)

struct CGPoint {
   CGFloat x;
   CGFloat y;
};
typedef struct CGPoint CGPoint;

As this is a plain C type, you cant use it, where you need to use objects. Wrap it in a NSValue object than, for example to store it into an array:

NSArray *points = @[[NSValue valueWithCGPoint:p1], [NSValue valueWithCGPoint:p2]]

I just had a look into Stephen G. Kochan's book in amazon. he is also sticking to the naming conventions, see page 96 program 6.2.

-setNumerator: as setter, -numerator as getter.

Although he is not using properties in this example, he most likely will also explain and use them later in the book.

share|improve this answer
    
Thank you so much for the great feedback again! I stick to name conventions next time :) Regarding the struct yes, I read about it a bit but he still didnt go over this in the book, I know he will later on (Just got to page 9). Thanks again, we need more people like you here and this site will be even more awesome ;) @vikingosegundo –  Nir Feb 14 '13 at 23:12
    
Just to emphasize again: naming conventions are very important, as different parts of the cocoa framework depend on them. –  vikingosegundo Feb 14 '13 at 23:52
    
Thanks! and I meant chapter 9 (not page:/) @vikingosegundo –  Nir Feb 15 '13 at 0:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.