This class is about defining class, memory management, and new feature, especially Property in Objective-C 2.0. 

define class is not familiar to C/C++ programmer, because its look is so different, We have to use '@interface' instead of 'class' and method cannot be in bracket, and method name and member variable's name can be equal. But it is not to hard to use. it is so easy!!. 

And memory management... it is really different from C/C++. There is global (This statement can be wrong) memory pool, called autorelease. It works by reference count (retain count) and it is not freed (dealloc) even though receive release message, if it's reference count (retain count) is not be 0. 

And property is also not familiar to me. i think is is similar with get {} / set {} in C#. it can reduce some boring code, and can be used by dot syntax. so convenient. 

Anyway, Here is my assignment 2-A

#import <Cocoa/Cocoa.h>



@interface PolygonShape : NSObject {

int numberOfSides;

int minimumNumberOfSides;

int maximumNumberOfSides;

float angleInDegrees;

float angleInRadians;

NSString* name;

}


- (void) dealloc;

- (id)init;

- (id)initWithNumberOfSides: (int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max; 

- (void) description;


@property (readwrite) int numberOfSides;

@property (readwrite) int minimumNumberOfSides;

@property (readwrite) int maximumNumberOfSides;

@property (readonly) float angleInDegrees;

@property (readonly) float angleInRadians;

@property (readonly) NSString* name;

@end



#import "PolygonShape.h"


@implementation PolygonShape


@synthesize numberOfSides;

@synthesize minimumNumberOfSides;

@synthesize maximumNumberOfSides;


- (void)dealloc

{

NSLog(@"dealloc is called. This Polygon ivar is destroyed");

[super dealloc];

}


- (void) setNumberOfSides: (int) value

{

if (value < minimumNumberOfSides)

{

NSLog(@"Invalid number of sides: %d is less than the maximum of %d allowed", value, minimumNumberOfSides);

}

else if (value > maximumNumberOfSides)

{

NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", value, maximumNumberOfSides);

}

else

{

numberOfSides = value;

}

}


- (void) setMinimumNumberOfSides: (int) value

{

if (value > 2)

{

minimumNumberOfSides = value;

}

else

{

NSLog(@"Invalid minimum number of sides: %d is less than the minimum of 2 allowed", value);

}

}


- (void) setMaximumNumberOfSides: (int) value

{

if (value <= 12)

{

maximumNumberOfSides = value;

}

else

{

NSLog(@"Invalid maximum number of sides: %d is greater than the maximum of 12 allowed", value);

}

}


- (id)init

{

[self initWithNumberOfSides:5 minimumNumberOfSides:3 maximumNumberOfSides:10];

return self;

}


- (id)initWithNumberOfSides: (int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max

{

[super init];

self.minimumNumberOfSides = min;

self.maximumNumberOfSides = max;

self.numberOfSides = sides;

return self;

}


- (float)angleInDegrees

{

return (180 * (numberOfSides - 2) / numberOfSides);

}


- (float)angleInRadians

{

return self.angleInDegrees * 2 * 3.14 / 360;

}


- (NSString*)name

{

NSMutableString* nameOfPolygon = [NSMutableString string];

switch (numberOfSides)

{

case 3 : nameOfPolygon = @"triangle"; break;

case 4 : nameOfPolygon = @"square"; break;

case 5 : nameOfPolygon = @"pentagon"; break;

case 6 : nameOfPolygon = @"hexagon"; break;

case 7 : nameOfPolygon = @"heptagon"; break;

case 8 : nameOfPolygon = @"octagon"; break;

case 9 : nameOfPolygon = @"enneagon"; break;

case 10 : nameOfPolygon = @"decagon"; break;

case 11 : nameOfPolygon = @"hendecagon"; break;

case 12 : nameOfPolygon = @"dodecagon"; break;

default : nameOfPolygon = @"";

}

return nameOfPolygon;

}


- (void)description

{

NSLog(@"Hello, I'm a %d-sided polygon (aka a %@) with angles of %f degrees (%f radians)", numberOfSides, self.name, self.angleInDegrees, self.angleInRadians);

}

@end



Posted by yunseong
이전버튼 1 2 3 4 5 6 7 8 9 10 ··· 39 이전버튼