Assignment2A-2
Assignment2A-2
Assignment
Now that we have some Objective-C experience under our belt, we’ll dive into the world of
custom classes and more advanced Objective-C language topics. You will define and use a new
custom class, and learn about Objective-C categories.
We will use the same WhatATool project from last week as our starting point. A few more
sections will be added to the existing structure.
The basic layout of your program should look something like this:
#import <Foundation/Foundation.h>
// sample function for one section, use a similar function per section
void PrintPathInfo() {
// Code from path info section here
}
PrintPathInfo(); // Section 1
PrintProcessInfo(); // Section 2
PrintBookmarkInfo(); // Section 3
PrintIntrospectionInfo(); // Section 4
PrintPolygonInfo(); // Section 6 (No function for section 5)
[pool release];
return 0;
}
Testing
In most assignments testing of the resulting application is the primary objective. In this case,
testing/grading will be done both on the output of the tool, but also on the code of each
section.
To help make the output of your program more readable, it would be helpful to put some kind
of header or separator between each of the sections.
Page 1 of 4
CS193P Assignment 2A
Spring 2009 Doll/Cannistraro
Assignment Walkthrough
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.
Page 2 of 4
CS193P Assignment 2A
Spring 2009 Doll/Cannistraro
3. Implement setter methods for each of the number of sides properties and enforce the
following constraints:
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:
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.
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:
9. In order to verify your memory management techniques, implement a dealloc method and
include an NSLog statement indicating that dealloc is being called.
Section Hints:
• You can find a list of names for polygons on the web. Wikipedia has a good one.
• The formula for computing the internal angle of a regular polygon in degrees is (180 *
(numberOfSides - 2) / numberOfSides).
• Remember your trigonometry: 360° is equal to 2π.
Page 3 of 4
CS193P Assignment 2A
Spring 2009 Doll/Cannistraro
IMPORTANT: In this section, you are expected to use +alloc and –init methods to create the
polygons and the array that they are put into. You must practice correct memory
management techniques of releasing the polygon objects when you are done with them.
3 7 4
5 9 6
9 12 12
When allocating your polygons, use a mixture of vanilla alloc/init then set the properties
along with using your custom initializer method that takes all of the properties in the
initializer method.
3. As you create each polygon, add them to the array of polygons and emit a log with the
polygon’s description. There should be at least 3 descriptions logged.
4. Test the constraints on your polygons. Iterate over the array of polygons and attempt to set
their numberOfSides properties to 10. This should generate two logs indicating that the
number of sides doesn’t fall within the constraints (for the first two polygons in the table
above).
5. Verify that your polygon objects are being deallocated correctly. If you have followed the
rules correctly with regard to memory management you should see 3 logs from the
dealloc method of your polygons. If you do not see these logs, review your alloc/init and
release calls to make sure they are correctly balanced for your polygon objects as well as
the array you are putting them into. Remember, alloc/init and release work like malloc
and free in C.
Page 4 of 4