Greg’s Online CS 193P Resources

April 17, 2009

Assignment 2A code snipets

Filed under: cs 193p,iPhone — gregorykaiser @ 10:10 pm
Tags:

Here is some of the code from 2A. It has many NSLog statements so I could see what was going on. Some are commented out. I also had a test function now commented out.

First we need to have PolygonShape .m and .h files to define our new class with it’s ivars and methods.

//
//  PolygonShape.h
//  WhatATool
//
//  Created by Gregory V Kaiser on 4/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import 


@interface PolygonShape : NSObject {
	int numberOfSides;
	int minimumNumberOfSides;
	int maximumNumberOfSides;
}

@property (readwrite) int numberOfSides;
@property (readwrite) int minimumNumberOfSides;
@property (readwrite) int maximumNumberOfSides;
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString * name;
@property (readonly) NSString * description;

- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
- (id)init;
- (void)dealloc;

@end
---------------------------------------------------------------

//
//  PolygonShape.m
//  WhatATool
//
//  Created by Gregory V Kaiser on 4/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PolygonShape.h"


@implementation PolygonShape
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max; {

	[self setMinimumNumberOfSides:(int)min];
	[self setMaximumNumberOfSides:(int)max];
	[self setNumberOfSides:(int)sides];
	return self;
}

- (id)init {
	return [self initWithNumberOfSides:5 minimumNumberOfSides:3 maximumNumberOfSides:10];
}	
	
@synthesize numberOfSides;
- (void)setNumberOfSides:(int)value {
	if (value = minimumNumberOfSides) {
		numberOfSides = value;
		//NSLog(@" In setNumberOf numberOfSides - numberOfSides: %d", numberOfSides);
	} else {
		NSLog(@" In setNumberOfSides - ERROR - sides:%d Is not in the valid range: %d to %d . So it is still %d", value, minimumNumberOfSides, maximumNumberOfSides, numberOfSides);
	}
}

@synthesize minimumNumberOfSides;
// add maxcheck later
- (void)setMinimumNumberOfSides:(int)value {
	if (value  (int)12) {
		maximumNumberOfSides = (int)12;
	} else {
		maximumNumberOfSides = value;
	}
	//NSLog(@" In maximunNumberOfSides value: %d  Returns: %d ", value, maximumNumberOfSides);
}

- (float) angleInDegrees; {
	return (float)((180.0 * (numberOfSides - 2)) / numberOfSides);
}

- (float) angleInRadians; {
	return (float)[self angleInDegrees] * ((3.1415926 * 2.0)/360.0);
}

- (NSString *)name; {
	NSArray *xlate = [NSArray arrayWithObjects:@"Bad0", @"Bad1", @"Bad2", @"Triangle", @"Square", @"Pentagon", @"Hexagon",
	@"Heptagon", @"Octagon", @"Nonagon", @"Decagone", @"Hendecagon", @"Dodecagon", nil]; 
	//NSString *firstColor = [colors objectForKey:@”Color 1”];
	
	return  (NSString *)[xlate objectAtIndex:numberOfSides];
}

- (NSString *)description; {
	NSLog(@"Hello I am a %d-sided polygone (aka a %@) with angles of %f degrees (%f radians)", self.numberOfSides, 
		  [self name], [self angleInDegrees], [self angleInRadians]);
	return  (NSString *)[self name];
}

- (void)dealloc {
	NSLog(@"DEALLOC called on polyShape");
	[super dealloc];
}
@end

Then we need to make use of the class in the main WhatATool.m program.

#import 
#import "PolygonShape.h"

// sample function for one section, use a similar function per section - copied from assignment sheet
void PrintPathInfo() { 
	// Code from path info section here 
	NSLog(@"Enter PrintPathInfo");
	
	NSString *aString = @"My Home Folder is at:";
	NSString *path = @"~";
	path = [path stringByExpandingTildeInPath];
	NSLog([aString stringByAppendingString: path]);
	
	NSInteger i = 0;
	NSArray *pathComponents = [path pathComponents];
	for (NSString *element in pathComponents) {
		NSLog(@"element %d: %@", i, element);
		i++;
	}
	NSLog(@"======================================================Path=>");
} 

void PrintProcessInfo() { 
	// Code 
	NSLog(@"Enter PrintProcessInfo");
	
	NSString *processName = [[NSProcessInfo processInfo] processName];
	// NSLog(processName);
	NSInteger processID  = [[NSProcessInfo processInfo] processIdentifier];
	// NSLog( @"%i", processID);
	NSLog(@"Process Name: %@    Process ID: %d  ", processName, processID);
	
	NSLog(@"===================================================Process=>");
} 
void PrintBookmarkInfo() { 
	// Code 
	NSLog(@"Enter PrintBookmarkInfo");
	NSMutableDictionary *bkmks = [NSMutableDictionary dictionaryWithCapacity:10];
	// NSURL *site = [ NSURL URLWithString: @"www.stanford.edu"];
	[ bkmks setObject:[ NSURL URLWithString: @"www.stanford.edu"] forKey:@"Stanford University"];
	[ bkmks setObject:[ NSURL URLWithString: @"www.apple.com"] forKey:@"Apple"];
	[ bkmks setObject:[ NSURL URLWithString: @"cs193p.stanford.edu"] forKey:@"CS193P"];
	[ bkmks setObject:[ NSURL URLWithString: @"itunes.stanford.edu"] forKey:@"Stanford on iTunes U"];
	[ bkmks setObject:[ NSURL URLWithString: @"stanfordshop.com"] forKey:@"Stanford Mall"];
	
	for (id key in bkmks) {
		if ([key hasPrefix:@"Stanford"])
			NSLog(@"Key: '%@', URL: '%@'", key, [bkmks objectForKey:key]);
	}
	
	NSLog(@"=======================================================Web=>");
} 

void PrintIntrospectionInfo() { 
	// Code 
	NSLog(@"Enter PrintIntrospectionInfo");
	
	NSString *path = @"~";
	path = [path stringByExpandingTildeInPath];
	NSArray *pathComponents = [path pathComponents];
	
	NSString *processName = [[NSProcessInfo processInfo] processName];
	// NSInteger processID  = [[NSProcessInfo processInfo] processIdentifier];	
	
	NSURL *site = [ NSURL URLWithString: @"www.stanford.edu"];
	NSMutableDictionary *bkmks = [NSMutableDictionary dictionaryWithCapacity:10];	
	
	NSMutableString *mstring1 = @"Mutable1";
	NSMutableString *mstring2 = @"THIS WAS ALL CAPS";
	
	NSMutableArray *marray = [NSMutableArray arrayWithCapacity:10];
	
	[marray addObject:path];
	[marray addObject:pathComponents];
	[marray addObject:processName];
	// adding NSInteger to mutable array causes warning that it is being changed to a pointer (??)
	// [marray addObject:processID];
	[marray addObject:site];
	[marray addObject:bkmks];
	[marray addObject:mstring1];
	[marray addObject:mstring2];
	
	// NSLog(@" %@", marray);

	NSUInteger limit = [marray count];
	NSUInteger i = 0;
	SEL sel = @selector(lowercaseString);
	for (i=0 ; i ");
} 

// following c functions added for assignment 2a
void TestPoly() {
	// Assignment 2a function to test PolyShape (will be commented out in main when things all work
	PolygonShape *p = [PolygonShape alloc];			// alloc
	[p setMinimumNumberOfSides:2];		// setter tests
	[p setMinimumNumberOfSides:3];	
	[p setMinimumNumberOfSides:4];
	NSLog(@"TestPoly:The Minimum Number of Sides: %d", [p minimumNumberOfSides]);	// getter test
	[p setMaximumNumberOfSides:11];	// setter
	[p setMaximumNumberOfSides:12];
	[p setMaximumNumberOfSides:13];
	NSLog(@"TestPoly:The Maximum Number of Sides: %d", [p maximumNumberOfSides]);	// getter
	[p setNumberOfSides:9];			// setter
	[p setNumberOfSides:99];
	[p setNumberOfSides:1];
	[p setNumberOfSides:10];
	NSLog(@"TestPoly:The Number of Side:s %d", [p numberOfSides]);	//getter
	[p autorelease];
	PolygonShape *p1 = [[PolygonShape alloc] initWithNumberOfSides:(int)3 minimumNumberOfSides:(int)3 maximumNumberOfSides:(int)11];	// alloc
	NSLog(@"TestPoly: initWithNumberOfSides called- Side:%d Min:%d Max:%d", p1.numberOfSides, p1.minimumNumberOfSides, p1.maximumNumberOfSides);
	NSLog(@"TestPoly: Sides: %d   angleInDegrees: %f  angleInRadians: %f name: %@", p1.numberOfSides, p1.angleInDegrees, p1.angleInRadians, p1.name);
	NSLog(@"TestPoly: Description: %@", [p1 description]);
	[p1 autorelease];
	PolygonShape *p2 = [[PolygonShape alloc] init];	// alloc
	NSLog(@"TestPoly: init called- Side:%d Min:%d Max:%d", p2.numberOfSides, p2.minimumNumberOfSides, p2.maximumNumberOfSides);
	NSLog(@"TestPoly: Sides: %d   angleInDegrees: %f  angleInRadians: %f name: %@", p2.numberOfSides, p2.angleInDegrees, p2.angleInRadians, p2.name);
	NSLog(@"TestPoly: Description: %@", [p2 description]);
	[p2 autorelease];
}	
// http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html#//apple_ref/doc/uid/20000051-SW5

void PrintPolygoneInfo() {
	// Section 6 code
	//1
	//NSMutableArray *marray = [NSMutableArray arrayWithCapacity:10];
	NSMutableArray *marray = [[NSMutableArray alloc] init];
	NSLog(@"DEBUG:Object Retain Count #1 of marray is:%d ",  [marray retainCount]);
	
	PolygonShape *p1 = [[PolygonShape alloc] init];			// alloc
	p1.minimumNumberOfSides = 3;
	p1.maximumNumberOfSides = 7;
	p1.numberOfSides = 4;
	NSLog(@"DEBUG:Object Retain Count of PolygonShape:%d is:%d ", 0, [p1 retainCount]);
	[marray addObject:p1];
	NSLog(@"DEBUG:After adding to marray Object Retain Count of PolygonShape:%d is:%d ", 0, [[marray objectAtIndex:0] retainCount]);
	//[marray addObject:[[PolygonShape alloc] initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7]];
	[[marray objectAtIndex:0] description];
	
	PolygonShape *p2 = [[PolygonShape alloc] initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9];			// alloc
	[marray addObject:p2];
	[[marray objectAtIndex:1] description];
	
	PolygonShape *p3 = [[PolygonShape alloc] initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];
	[marray addObject:p3];
	[[marray objectAtIndex:2] description];
	
	int i, count;
	NSLog(@"DEBUG:Object Retain Count #2 of marray is:%d ",  [marray retainCount]);
	count = [marray count];
	NSLog(@"DEBUG:Object Retain Count #3 of marray is:%d ",  [marray retainCount]);
	for (i = 0; i < count; i++) {
		//NSLog(@"Object Retain Count of PolygonShape:%d is:%d ", i, [[marray objectAtIndex:i] retainCount]);
		[[marray objectAtIndex:i] setNumberOfSides:10];
	}
	//[marray release];
	NSLog(@"DEBUG:Object Retain Count #4 of marray is:%d ",  [marray retainCount]);
	[marray release];
	NSLog(@"DEBUG:Object Retain Count of PolygonShape:%d is:%d ", 0, [p1 retainCount]);
	[p1 release];
	[p2 release];
	[p3 release];
	
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
	NSLog(@"================WhatATool(Part 2a)===========================");
	
	//code for cs193p Assignment 1
	PrintPathInfo();           // Section 1 
	PrintProcessInfo();        // Section 2 
	PrintBookmarkInfo();       // Section 3 
	PrintIntrospectionInfo();  // Section 4 
	// Section 5 are PolygonShape.m .h files
	// added for assignment 2a    Section 6
	//TestPoly();				   // To test PolyShape, I comment this out as well as NSLog msgs in Polygone setter methods for final proj
	PrintPolygoneInfo();
	
	NSLog(@"=====================END====================================");

    [pool drain];  //drain or release?? I think drain - see above document
    return 0;
}
Advertisements

1 Comment »

  1. Thanks for your efforts, I learn better by deconstructing and your code has helped alot.

    Thanks..

    Comment by quicka2 — April 22, 2009 @ 2:20 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: