Hi everyone!
So i've been trying to learn to programme for the iphone and i've been doing some of the assignments on the university of stanford website (www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winterg)
Ive been having trouble with assgnment 2a could someone help? It compiles but doesn't work when i run it.
and the implementation:
So i've been trying to learn to programme for the iphone and i've been doing some of the assignments on the university of stanford website (www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winterg)
Ive been having trouble with assgnment 2a could someone help? It compiles but doesn't work when i run it.
Code:
// PolygonShape.h
#import <Cocoa/Cocoa.h>
@interface PolygonShape : NSObject
{
//declare variables
int numberOfSides;
int minimumNumberOfSides;
int maximumNumberOfSides;
}
//declare properties
@property int numberOfSides;
@property int minimumNumberOfSides;
@property int maximumNumberOfSides;
@property(readonly) float angleInDegrees;
@property(readonly) float angleInRadians;
@property(readonly) NSString *name;
//define methods
-(id)initWithNumberOfSides:(int)num minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
-(id)init;
-(NSString*)description;
-(void)dealloc;
@end
and the implementation:
Code:
// PolygonShape.m
#import "PolygonShape.h"
//implement methods
@implementation PolygonShape
@synthesize numberOfSides=num, minimumNumberOfSides=min, maximumNumberOfSides=max;
-(id)initWithNumberOfSides:(int)num minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
{
[self setNumberOfSides:num];
[self setMinimumNumberOfSides:min];
[self setMaximumNumberOfSides:max];
return self;
}
-(id)init
{
return [self initWithNumberOfSides:5 minimumNumberOfSides:3 maximumNumberOfSides:10];
}
-(void)setNumberOfSides:(int)newnum
{
if(([self minimumNumberOfSides]<newnum)&&(newnum<[self maximumNumberOfSides])){
num=newnum;
}
else {
NSLog(@"Out of range-Number of sides must be between %@ and %@", [self minimumNumberOfSides], [self maximumNumberOfSides]);
}
}
-(void)setminimumNumberOfSides:(int)newmin
{
if(newmin > 2){
num = newmin;
}
else {
NSLog(@"Out of range-Number of sides must be greater than %@", newmin);
}
}
-(void)setmaximumNumberOfSides:(int)newmax
{
if(newmax <= 12){
num = newmax;
}
else {
NSLog(@"Out of range-Number of sides must be less than or equal to %@", newmax);
}
}
-(float) angleInDegrees;
{
int p = num;
float angleInDegrees = (((p-2)/p)*180);
return angleInDegrees;
}
-(float)getangleInRadians
{
int p = num;
float angleInRadians = (((p-2)/p)*3.1416);
return angleInRadians;
}
-(NSString*)name
{
NSString *nameOfPolygon=@"";
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:
break;
}
return nameOfPolygon;
}
-(NSString*)description
{
NSLog(@"Hello i am a %@-sided polygon (aka a %@) with angle of %@ degrees(%@ radians)", numberOfSides, [self name],[self angleInDegrees], [self angleInRadians]);
}
@end