Skip to content

Instantly share code, notes, and snippets.

@jduff
Created September 17, 2009 20:08
Show Gist options
  • Save jduff/188680 to your computer and use it in GitHub Desktop.
Save jduff/188680 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface PolygonShape : NSObject {
int numberOfSides;
int minimumNumberOfSides;
int maximumNumberOfSides;
}
@property (assign) int numberOfSides;
@property (assign) int minimumNumberOfSides;
@property (assign) int maximumNumberOfSides;
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString* name;
- (id)init;
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
- (NSString *)description;
@end
#import "PolygonShape.h"
@implementation PolygonShape
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
-(void) setNumberOfSides:(int)value{
if(value < self.minimumNumberOfSides)
NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed", value, self.minimumNumberOfSides);
else if(value > self.maximumNumberOfSides)
NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", value, self.maximumNumberOfSides);
else
numberOfSides = value;
}
-(void) setMinimumNumberOfSides:(int)value{
if(value <= 2)
NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed", value, 2);
else
minimumNumberOfSides = value;
}
-(void) setMaximumNumberOfSides:(int)value{
if(value > 12)
NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", value, 12);
else
maximumNumberOfSides = value;
}
- (float) angleInDegrees {
return (180.0*(self.numberOfSides-2)/self.numberOfSides);
}
- (float) angleInRadians {
return (self.angleInDegrees * M_PI)/180;
}
- (NSString *) name {
NSArray* names = [NSArray arrayWithObjects:@"Triangle", @"Square", @"Pentagon", @"Hexagon",@"Heptagon",@"Octagon",
@"Nonagon",@"Decagon",@"Hendecagon", @"Dodecagon",nil];
return self.numberOfSides<3 ? @"Unknown" : [names objectAtIndex: self.numberOfSides-3];
}
- (NSString *) description{
return [NSString stringWithFormat: @"Hello I am a %d-sided polygon (aka a %@) with angles of %.0f degrees (%f radians)",
self.numberOfSides, self.name, self.angleInDegrees, self.angleInRadians];
}
- (id)init {
return [self initWithNumberOfSides:5 minimumNumberOfSides:3 maximumNumberOfSides:12];
}
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max {
// allow superclass to initialize its state first
if (self = [super init]) {
self.maximumNumberOfSides = max;
self.minimumNumberOfSides = min;
self.numberOfSides = sides;
}
return self;
}
-(void) dealloc {
NSLog(@"Calling dealloc");
[super dealloc];
}
@end
void PrintPolygonInfo() {
NSMutableArray* polygons = [[NSMutableArray alloc] init];
PolygonShape* poly1 = [[PolygonShape alloc] init];
[polygons addObject:poly1];
NSLog([poly1 description]);
PolygonShape* poly2 = [[PolygonShape alloc] initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7];
[polygons addObject:poly2];
NSLog([poly2 description]);
PolygonShape* poly3 = [[PolygonShape alloc] init];
poly3.minimumNumberOfSides=5;
poly3.maximumNumberOfSides=9;
poly3.numberOfSides=6;
[polygons addObject:poly3];
NSLog([poly3 description]);
PolygonShape* poly4 = [[PolygonShape alloc] initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];
[polygons addObject:poly4];
NSLog([poly4 description]);
for(PolygonShape* shape in polygons) {
shape.numberOfSides=10;
[shape release];
}
NSLog(@"All Shape Instances Released");
[polygons release]; // you don't see the dealloc calls until this happens, ie. the array retained the objects
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment