1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#import "PolygonShape.h"
#import "PolygonView.h"

@implementation PolygonShape

@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
@synthesize angleInDegrees;
@synthesize angleInRadians;

- (int)angleInDegrees {
	int result = (180 * (numberOfSides -2) / numberOfSides);
		
	return result;
}

- (int)angleInRadians {
	int degrees = angleInDegrees;
	int result = degrees * 0.0174532925;
	
	return result;
}


- (NSString *)name {
    NSArray *polygons = [NSArray arrayWithObjects:
						 @"Bad0", @"Bad1", @"Bad2",
						 @"Triangle", @"Square", @"Pentagon",
						 @"Hexagon", @"Heptagon", @"Octagon",
						 @"Nonagon", @"Decagone", @"Hendecagon",
						 @"Dodecagon", nil];

    return [polygons objectAtIndex:numberOfSides];
}


- (NSString *)description {
	return [[NSString alloc] initWithFormat:@"Hello I am a %i-sided polygon (aka a %@) with angles of %i degrees (%i radians).", numberOfSides, self.name, self.angleInDegrees, self.angleInRadians];
}

- (void)setNumberOfSides:(int)newNumberOfSides {	
	if (newNumberOfSides != numberOfSides) {
		if ((newNumberOfSides > minimumNumberOfSides) && (newNumberOfSides < maximumNumberOfSides)) {
			numberOfSides = newNumberOfSides;
		} else {
			NSLog(@"setNumberOfSides: Invalid number of sides: %i is greater than the maximum of %i or smaller than the minimum of %i allowed.",
				  newNumberOfSides, maximumNumberOfSides, minimumNumberOfSides);
		}
	}
}

- (void)setMinimumNumberOfSides:(int)newMinimumNumberOfSides {
	if (newMinimumNumberOfSides != minimumNumberOfSides) {	
		int minimum = 2;
		 
		if (newMinimumNumberOfSides > minimum) {
			minimumNumberOfSides = newMinimumNumberOfSides;
		} else {
			NSLog(@"setMinimumNumberOfSides: Invalid number of sides: %i is smaller than the minimum of %i allowed.",
				  newMinimumNumberOfSides, minimum);
		}
	}
}

- (void)setMaximumNumberOfSides:(int)newMaximumNumberOfSides {	
	if (newMaximumNumberOfSides != maximumNumberOfSides) {
		int maximum = 12;
		
		if (newMaximumNumberOfSides <= maximum) {
			maximumNumberOfSides = newMaximumNumberOfSides;
		} else {
			NSLog(@"setMaximumNumberOfSides: Invalid number of sides: %i is greater than the maximum of %i allowed.",
				  newMaximumNumberOfSides, maximum);
		}
	}
}

- (id)init {
	int minimum = 3;
	int maximum = 10;
	int sides = 8;
	
	[self initWithNumberOfSides:(int)sides
		   minimumNumberOfSides:(int)minimum
		   maximumNumberOfSides:(int)maximum
	 ];
	
	return self;
}

- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max {
	self.minimumNumberOfSides = min;
	self.maximumNumberOfSides = max;
	self.numberOfSides = sides;
	
	return self;
}

- (void)dealloc {
	NSLog(@"---");
	NSLog(@"dealloc is being called!");

	[name release];
	[super dealloc];
}

@end