Laboratory‎ > ‎Objective-C‎ > ‎

WhatATool

This program is part of Stanford CS193P Assignment 1B and 2A. To be used by independent developers solely for own development purpose.


WhatATool.m

A main implementation file for execution of basic Objective-C language topics

#import <Foundation/Foundation.h>
#import "PolygonShape.h"
 
void PrintPathInfo(){
	// Code from path info section here
	NSString *path = @"/Users/Hoonio";
	NSLog(@"My home folder is at '%@'", path);
	NSArray *pathComponents = [path pathComponents];
	for(int i=0; i < [pathComponents count]; i++){
		NSLog(@"%@", [pathComponents objectAtIndex:i]);
	}
}
 
void PrintProcessInfo(){
	NSString *processName = [[NSProcessInfo processInfo] processName];
	int processID = [[NSProcessInfo processInfo] processIdentifier];
	NSLog(@"Process Name: '%@' Process ID: '%i'", processName, processID);
}
 
void PrintBookmarkInfo(){
	NSArray *keys = [NSArray arrayWithObjects:@"Hoonio", @"ViciHooni", @"Sentimentum", @"Apple", @"CS193P", nil];
	NSArray *urls = [NSArray arrayWithObjects:[NSURL URLWithString:@"http://www.hoonio.com"], [NSURL 
            URLWithString:@"http://wiki.hoonio.com"], [NSURL URLWithString:@"http://blog.hoonio.com"], [NSURL 
            URLWithString:@"http://www.apple.com"], [NSURL URLWithString:@"http://cs193p.stanford.edu"], nil];
	NSMutableDictionary *dictionary = [NSDictionary dictionaryWithObjects:urls forKeys:keys];
 
	for (id key in dictionary){
		if ([key hasPrefix:@"Hoonio"]) {
			NSLog(@"Key: '%@' URL: '%@'", key, [dictionary objectForKey:key]);
		}
	}
}
 
void PrintIntrospectionInfo(){
	NSArray *keys = [NSArray arrayWithObjects:@"Hoonio", @"ViciHooni", @"Sentimentum", nil];
	NSArray *objects = [NSArray arrayWithObjects:@"hello world!", [NSURL URLWithString:@"http://www.hoonio.com"], 
            [NSDictionary dictionary], nil];
	NSMutableDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
 
	for (id key in dictionary){
		id object = [dictionary objectForKey:key];
		NSLog(@"Class name: %@", [object class]);
		if ([object isMemberOfClass:[NSString class]]) {
			NSLog(@"conditional entered");
		}
		NSLog(@"Is Member of NSString: %@", ([object isMemberOfClass:[NSString class]] ? @"YES" : @"NO"));
		NSLog(@"Is Kind of NSString: %@", ([object isKindOfClass:[NSString class]] ? @"YES" : @"NO"));
		if ([object respondsToSelector:@selector(lowercaseString)]) {
			NSLog(@"Responds to lowercaseString: YES");
			NSLog(@"lowercaseString is: %@", object);
		}
		else {
			NSLog(@"Responds to lowercaseString: NO");
		}
	}
 
}
 
void PrintPolygonInfo(){
	NSMutableArray *polygonArray = [[NSMutableArray alloc] initWithCapacity:3];
 
	PolygonShape *poly1 = [[PolygonShape alloc] initWithNumberOfSides:4 minimumNumberOfSides:3 
            maximumNumberOfSides:7];
	[polygonArray addObject:poly1]; 
	[poly1 printOutput];
	[poly1 release];
	PolygonShape *poly2 = [[PolygonShape alloc] initWithNumberOfSides:6 minimumNumberOfSides:5 
            maximumNumberOfSides:9];
	[polygonArray addObject:poly2]; 
	[poly2 printOutput];
	[poly2 release];
	PolygonShape *poly3 = [[PolygonShape alloc] initWithNumberOfSides:12 minimumNumberOfSides:9 
            maximumNumberOfSides:12];
	[polygonArray addObject:poly3]; 
	[poly3 printOutput];
	[poly3 release];
 
	NSUInteger index = 0;
	// attempt to set numberOfSides to 10
	NSLog(@"Testing for setter iteration");
	for (id element in polygonArray){
		NSLog(@"Attempting to set numberOfSides for polygon %u", index);
		[element setNumberOfSides:10];
		if (++index >= 3)
			break;
	}
 
	NSLog(@"Polygon Array deallocated");
 
	[polygonArray release];
}
 
 
 
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    // insert code here...
	PrintPathInfo();			// Section 1
	PrintProcessInfo();			// Section 2
	PrintBookmarkInfo();		// Section 3
    PrintIntrospectionInfo();	// Section 4
	PrintPolygonInfo();			// Section 6
 
	[pool release];
    return 0;
}


PolygonShape

A custom class for polygon information storage

//
//  PolygonShape.m
//  WhatATool
//
//  Created by Hoonio on 2/27/10.
//  Copyright 2010 Hoonio.com. All rights reserved.
//
 
#import "PolygonShape.h"
 
 
@implementation PolygonShape
 
@synthesize minimumNumberOfSides, maximumNumberOfSides, numberOfSides;
 
- (void)setMinimumNumberOfSides:(int)numberOfSidesSet {
	if (numberOfSidesSet < 3)
		NSLog(@"Invalid number of sides: %u is less than the minimum of 3 allowed", numberOfSidesSet);
	minimumNumberOfSides = numberOfSidesSet;
}
 
- (void)setMaximumNumberOfSides:(int)numberOfSidesSet {
	if (numberOfSidesSet > 12)
		NSLog(@"Invalid number of sides: %d is greater than the maximum of 12 allowed", numberOfSidesSet);
	maximumNumberOfSides = numberOfSidesSet;
}
 
- (void)setNumberOfSides:(int)numberOfSidesSet {
	NSLog(@"Setting number of sides as %d over %d", numberOfSidesSet, numberOfSides);
	if (numberOfSidesSet < minimumNumberOfSides)
		NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed", numberOfSidesSet, 
                    minimumNumberOfSides);
	if (numberOfSidesSet > maximumNumberOfSides)
		NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", numberOfSidesSet, 
                    maximumNumberOfSides);
	numberOfSides = numberOfSidesSet;
}
 
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max{
	// allow superclass to initialize its state first
	if (self = [super init]){
		// do polygon initialization
		if (min < 3)
			NSLog(@"Invalid number of sides: %u is less than the minimum of 3 allowed", min);
		minimumNumberOfSides = min;
		if (max > 12)
			NSLog(@"Invalid number of sides: %u is greater than the maximum of 12 allowed", max);
		maximumNumberOfSides = max;
		if (!(min < sides < max))
			NSLog(@"Invalid number of sides: %u is not within the defined range %u - %u", sides, min, max);
		numberOfSides = sides;
	}
 
	return self;
}
 
// overriding custom initializer with default values
- (id)init{
	// allow superclass to initialize its state first
	if (self = [super init]){
		// do polygon initialization
		numberOfSides = 5;
		minimumNumberOfSides = 3;
		maximumNumberOfSides = 10;
	}
	return self;
}
 
- (float)angleInDegrees{
	return 180 * (numberOfSides - 2) / numberOfSides;
}
 
- (float)angleInRadians{
	return M_PI * (numberOfSides - 2) / numberOfSides;
}
 
- (NSString *)name{
	NSArray *polygonName = [NSArray arrayWithObjects:@"nothing", @"henagon", @"Digon", @"Triangle", @"Square", 
            @"Pentagon", @"Hexagon", @"Heptagon", @"Octagon", @"Ennegon", @"Decagon", @"Hendecagon", @"Dodecagon", nil];
	return [polygonName objectAtIndex:numberOfSides];
}
 
- (void)printOutput{
	NSLog(@"Hello I am a %d-sided polygon (aka a %@) with angles of %f degrees (%f radians).", numberOfSides, 
            self.name, self.angleInDegrees, self.angleInRadians);
}
 
- (void)dealloc{
	// Do any cleanup that’s necessary
	NSLog(@"Termination: Polygon shape dealloc");
	// when we’re done, call super to clean us up
	[super dealloc];
}
 
@end
//
//  PolygonShape.h
//  WhatATool
//
//  Created by Hoonio on 2/27/10.
//  Copyright 2010 Hoonio.com. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
 
@interface PolygonShape : NSObject {
	int numberOfSides;
	int minimumNumberOfSides;
	int maximumNumberOfSides;
}
 
@property int minimumNumberOfSides;
@property int maximumNumberOfSides;
@property int numberOfSides;
 
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString *name;
 
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
- (void)init;
 
- (void)printOutput;
- (void)dealloc;
 
@end



Comments