Lab 3 - Objective-C Classes
Objectives
- Create a custom class
- Use basic memory management
You will be creating a new custom class called PolygonShape.
- In Xcode, create a new Project. To create the files for the new
class in Xcode first choose File > New File... Then in the Cocoa
section under MacOS X select the 'Objective-C class' template. Name
the file PolygonShape.m. Be certain the checkbox to create a header
file is also checked. This creates a class that will inherit from
NSObject by default.
- 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
- The numberOfSides, minimumNumberOfSides and maximumNumberOfSides
properties should all be backed by instance variables and be synthesized so
that the compiler generates the accessor methods. The readonly properties
will only generate a getter method but not a setter method.
- Implement setter methods for each of the number of sides properties that
enforce the following constraints:
- numberOfSides must be between the minimum and maximum number of
sides
- minimumNumberOfSides must be greater than 2
- maximumNumberOfSides must be less than or equal to 12
Attempts to set one of the properties outside of the constraints should
fail and log an error message something like:
Invalid number of sides: 9 is
greater than the maximum of 5 allowed
- 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.
- Implement a custom init method which will override the inheritied
NSObject one which calls your custom initializer with default values of a 5
sided polygon with min of 3 sides and max of 10 sides.
- The angleInDegrees and angleInRadians properties should not be
stored in instance variables since they are derived by the numberOfSides.
They do not need to be synthesized and you should implement methods for each
of them which return the appropriate values. We are using regular
polygons so the angles are all the same.
The formula for computing the internal angle of a regular polygon in degrees
is (180 * (numberOfSides - 2) / numberOfSides
Remember that 360 degrees is equal to 2*pi
- The name property should also not be synthesized nor stored in an
instance varialbe 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, "Triangle" and "Square" for 3-sided and 4-sided polygons.
You can find a list of names for polygons on the web. Wikipedia has a
good one.
- Write a method -description for your class. For example the output
might look like:
I am a 4-sided polygon (aka a Square) with angles of 90
degrees (1.570796 radians).
- Implement a dealloc method which includes an NSLog statement (so when
can check when it is being invoked).
- In your main program, create a mutable array (using alloc/init) and
create 3 or more PolygonShape instances with the following values:
min 3, max 7,sides 4
min 5 max 9, sides 6
min 9, max 12, sides 12
As you create each polygon, add them to the array and log the polygon's
description.
Test the constraints by interating over the array and attempt to set the
number of sides to 10. This should log two errors.
Finally, release each of the polygons in the array, and the array itself and
verify that you generated the proper log messages.
- Compress your project and send it to me through the Dropbox in
BlackBoard.