Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cb1

macrumors newbie
Original poster
Jun 30, 2010
9
0
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.


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
 
Edit the first post with
Code:
code in here, using code and /code in brackets
(This will keep the forum from making your :( be frownies!)
 
the assignment is to:

Create a PolygonShape class.
To create the files for the new class in Xcode:
1. Choose File > New File...
2. In the Cocoa section under Mac OS X, select the ‘Objective-C class’ template
3. Name the file PolygonShape.m – Be certain the checkbox to create a header file is also
checked
Note that your new class inherits from NSObject by default, which happens to be what we want.
Now that we’ve got a class, we need to give it some attributes. Objective-C 2.0 introduced a new mechanism for specifying and accessing attributes of an object. Classes can define “properties” which can be accessed by users of the class. While properties usually allow developers to avoid having to write boilerplate code for setting and getting attributes in their classes, they also allow classes to express how an attribute can be used or what memory management policies should be applied to a particular attribute. For example, a property can be defined to be read-only or that an when an object property is set how the ownership of that object should be handled.
In this section you will add some properties to your PolygonShape class.

1. Add the following properties to your PolygonShape class
• numberOfSides – an int value • minimumNumberOfSides – an int value • maximumNumberOfSides – an int value • angleInDegrees – a float value, readonly • angleInRadians – a float value, readonly • name – an NSString object, readonly

2. The numberOfSides, minimumNumberOfSides and maximumNumberOfSides properties should all be backed by instance variables of the appropriate type. These properties should all be synthesized. When a property is “synthesized” the compiler will generate accessor methods for the properties according to the attributes you specify in the @property declaration. For example, if you specified a property as “readonly” then the compiler will only generate a getter method but not a setter method.

3. Implement setter methods for each of the number of sides properties and enforce the following constraints:
• numberOfSides – between the minimum and maximum number of sides
• minimumNumberOfSides – greater than 2
• maximumNumberOfSides – less than or equal to 12
Attempts to set one of these properties outside of the constraints should fail and log an error message. For example, if you have a polygon that is configured with a maximumNumberOfSides set to 5 and you attempt to set the numberOfSides property to 9 you should log a message saying something like:
Invalid number of sides: 9 is greater than the maximum of 5 allowed

4. Implement a custom initializer method that takes the number of sides for the polygon:
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
Your initializer should set the minimum and maximum number of sides first (to establish the constraints) and then set the number of sides to the value passed in.

5. Implement a custom init method (overriding the version implemented in NSObject) which calls your custom initializer with default values. For example, your generic -[PolygonShape init] method might create a 5 sided polygon with min of 3 sides and max of 10.

6. The angleInDegrees and angleInRadians properties should not be stored in an instance variable since they are all properties derived by the numberOfSides. These properties do not need to be synthesized and you should implement methods for each of them which return the appropriate values. We’re being boring and using regular polygons so the angles are all the same.

7. Similarly, the name property should also not be synthesized (nor stored in an instance variable) and you should implement a method for it. The name of the polygon should be a descriptive name for the number of sides. For example, if a polygon has 3 sides it is a “Triangle”. A 4-sided polygon is a “Square”.

8. Give your PolygonShape class a -description method. Example output from this method:
Hello I am a 4-sided polygon (aka a Square) with angles of 90 degrees (1.570796 radians).

9. In order to verify your memory management techniques, implement a dealloc method and include an NSLog statement indicating that dealloc is being called.
 
the assignment is to:

clipped...
Okay, that's the assignment. Doesn't really answer my question, though. Your code, as it exists: what does it do? Any console output? Runtime errors? What are you expecting it to do and what is different from your expectations?
 
oh sorry.
well after i created an instance of the polygon class it printed:

Out of range-Number of sides must be betweennull and null

this came up regardless of the dimensions i used.
also i tried using the "description" method but it didn't work.

This is from my main file:

Code:
PolygonShape *triangle = [[PolygonShape alloc] init];

[triangle initWithNumberOfSides:3 minimumNumberOfSides:3 maximumNumberOfSides:7];
	
[triangle description];






when it runs it prints "Segmentation Fault"
I've no idea what that means?
 
This method has a logical error:

Code:
-(id)initWithNumberOfSides:(int)num minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
{
	
  [self setNumberOfSides:num];
  [self setMinimumNumberOfSides:min];
  [self setMaximumNumberOfSides:max]; 
  return self;

}

The various set methods are called in the order you specify. So first you call setNumberOfSides. At this point min and max number of sides are not set so default to 0. This causes the issue.

This method is also likely to cause issues as it does not call
Code:
[super init]
which all init methods should call.

The reason you are getting the nulls in "Out of range-Number of sides must be betweennull and null" is that you are using %@ as the place holder. This is used for objects. Your properties are ints. So you are trying to use the int as a pointer. This is very bad indeed (I would expect program crashes from this). You need to change it to the correct specifier for an int.
 
Thank you so much that helped a lot. There was a few other mistakes but i've fixed it all now. Thanks all!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.