スタンフォード大学のiPhoneアプリ開発講座の宿題(その3)

宿題その3(Command Line Tool II)の自分なりの回答を以下に記載しておく。

  • PolygonShape.h*1
#import <Cocoa/Cocoa.h>

@interface PolygonShape : NSObject {
    int numberOfSides;
    int minimumNumberOfSides;
    int maximumNumberOfSides;
}

@property int numberOfSides;
@property int minimumNumberOfSides;
@property int maximumNumberOfSides;
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString *name;

- (id)initWithNumberOfSides:(int)sides
        minimumNumberOfSides:(int)min
        maximumNumberOfSides:(int)max; 
@end
  • PolygonShape.m
#import "PolygonShape.h"

#define MinNumberOfSides	3
#define MaxNumberOfSides	12

@implementation PolygonShape
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;

- (id)initWithNumberOfSides:(int)sides
        minimumNumberOfSides:(int)min
        maximumNumberOfSides:(int)max {
    if (self = [super init]) {
        [self setMinimumNumberOfSides:min];
        [self setMaximumNumberOfSides:max];
        [self setNumberOfSides:sides];
    }
    return self;
}

- (id)init {
    return [self initWithNumberOfSides:5
                    minimumNumberOfSides:3
                    maximumNumberOfSides:10];
}

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

- (void)setMinimumNumberOfSides:(int)number {
    if (number < MinNumberOfSides) {
        NSLog(@"%d is smaller than the minimum of %d allowed",
                    number, MinNumberOfSides);
    } else {
        minimumNumberOfSides = number;
    }
}

- (void)setMaximumNumberOfSides:(int)number {
    if (MaxNumberOfSides < number) {
        NSLog(@"%d is greater than the maximum of %d allowed",
                    number, MaxNumberOfSides);
    } else {
        maximumNumberOfSides = number;
    }
}

- (void)dealloc {
    NSLog(@"dealloc is being called");
    [super dealloc];
}

- (float)angleInDegrees {
    return (180 * (numberOfSides - 2) / numberOfSides);
}

- (float)angleInRadians {
    return ([self angleInDegrees] / 360) * (2 * M_PI);
}

- (NSString*)name {
    NSString *name;
    switch (numberOfSides) {
        case 3:
            name = @"Triangle"; break;
        case 4:
            name = @"Quadrilateral"; break;
        case 5:
            name = @"Pentagon"; break;
        case 6:
            name = @"Hexagon"; break;
        case 7:
            name = @"Heptagon"; break;
        case 8:
            name = @"Octagon"; break;
        case 9:
            name = @"Enneagon"; break;
        case 10:
            name = @"Decagon"; break;
        case 11:
            name = @"Hendecagon"; break;
        case 12:
            name = @"Dodecagon"; break;
        default:
            break;
    }
    return name;
}

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

*1:#import の部分は、HelloPolyで使う際には#import に変える必要がある。