Familiarizing Yourself With Iphone Development Stanford Exercise Whatatoolb
Stanford Exercise 2B - WhatAToolB Here's my solution and code for this exercise: WhatAToolB.m #import #import "PolygonShape....
https://www.czetsuyatech.com/2010/12/xcode-whatatoolb-stanford.html
Stanford Exercise 2B - WhatAToolB
Here's my solution and code for this exercise:
WhatAToolB.m
Here's my solution and code for this exercise:
WhatAToolB.m
#importPolygonShape.h#import "PolygonShape.h" void PrintPathInfo() { //expand the directory NSString *path = [@"~" stringByExpandingTildeInPath]; NSLog(@"My home folder is at %@", path); //get the components of the url NSArray *folders = [path pathComponents]; //print each component/folder for(NSString *element in folders) { NSLog(@"%@", element); } } void PrintProcessInfo() { NSString *processName = [[NSProcessInfo processInfo] processName]; NSInteger processIdentifier = [[NSProcessInfo processInfo] processIdentifier]; //prints the process name and id NSLog(@"Process Name: '%@' Process ID: '%i'", processName, processIdentifier); } void PrintBookmarkInfo() { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //creates an NSURL object and add it to a NSMutableDictionary object [dictionary setValue: [NSURL URLWithString:@"http://www.stanford.edu"] forKey: @"Stanford University"]; [dictionary setValue: [NSURL URLWithString:@"http://www.apple.com"] forKey: @"Apple"]; [dictionary setValue: [NSURL URLWithString:@"http://www.cs193p.stanford.edu"] forKey: @"CS193P"]; [dictionary setValue: [NSURL URLWithString:@"http://www.itunes.stanford.edu"] forKey: @"Stanford on iTunes U"]; [dictionary setValue: [NSURL URLWithString:@"http://www.stanfordshop.com"] forKey: @"Stanford Mall"]; //iterate NSString *key; for(key in dictionary) { NSString *value = [dictionary objectForKey:key]; //checks if prefix has "Stanford" word if([key hasPrefix: @"Stanford"]) { NSLog(@"%@", value); } } } void PrintIntrospectionInfo() { //creates an assorted types of objects NSString *string = [NSString stringWithFormat:@"Hello World!"]; NSURL *url = [NSURL URLWithString:@"http://google.com"]; NSProcessInfo *processInfo = [NSProcessInfo processInfo]; NSDictionary *dictionary = [NSDictionary dictionary]; NSMutableString *ms1 = [NSMutableString string]; [ms1 appendString:@"Hello "]; [ms1 appendString:@"World!"]; NSMutableArray *array = [NSMutableArray array]; //add to an object array [array addObject: string]; [array addObject: url]; [array addObject: processInfo]; [array addObject: dictionary]; [array addObject: ms1]; //iterate to the object array int count = [array count]; for(int i = 0; i < count; i++) { //get the object at the current index NSObject *object = [array objectAtIndex: i]; //prints the class name NSLog(@"Class name: %@", [object className]); //check if the object is a member of class NSString if([object isMemberOfClass: [NSString class]]) { NSLog(@"Is Member of NSString: YES"); } else { NSLog(@"Is Member of NSString: NO"); } //check if the object is a kind of NSString class if([object isKindOfClass: [NSString class]]) { NSLog(@"Is Kind of NSString: YES"); } else { NSLog(@"Is Kind of NSString: NO"); } //initialize a lowercaseString selector SEL selector = @selector(lowercaseString); //check if the object respond to that selector if([object respondsToSelector: selector]) { NSLog(@"Responds to lowercaseString: YES"); //perform the method NSLog(@"lowercaseString is: %@", [object performSelector: selector]); } else { NSLog(@"Responds to lowercaseString: NO"); } NSLog(@"=================================="); } } void PrintPolygonInfo() { NSMutableArray *arrayPoly = [[NSMutableArray alloc] init]; PolygonShape *shape1 = [[PolygonShape alloc] init]; [shape1 initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7]; [arrayPoly addObject:shape1]; NSLog(@"Poly1: %@", shape1); [shape1 retain]; [shape1 initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9]; [arrayPoly addObject:shape1]; NSLog(@"Poly1: %@", shape1); [shape1 retain]; [shape1 initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12]; [arrayPoly addObject:shape1]; NSLog(@"Poly1: %@", shape1); int count = [arrayPoly count]; for(int i = 0; i < count; i++) { PolygonShape *temp = [arrayPoly objectAtIndex:i]; [temp setNumberOfSides:10]; } [shape1 release]; [shape1 release]; [shape1 release]; } int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello, World!"); PrintPathInfo(); PrintProcessInfo(); PrintBookmarkInfo(); PrintIntrospectionInfo(); PrintPolygonInfo(); [pool drain]; return 0; }
// // PolygonShape.h // WhatAToolB // // Created by edwardlegaspi on 12/16/10. // Copyright 2010 Kalidad Business Solutions. All rights reserved. // #importPolygonShape.m@interface PolygonShape : NSObject { int numberOfSides; int minimumNumberOfSides; int maximumNumberOfSides; float angelInDegrees; float angelInRadians; NSString *name; } @property int numberOfSides; @property int minimumNumberOfSides; @property int maximumNumberOfSides; @property (readonly) float angelInDegrees; @property (readonly) float angelInRadians; @property (readonly) NSString *name; -(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max; -(NSString *)description; @end
//
// PolygonShape.m
// WhatAToolB
//
// Created by edwardlegaspi on 12/16/10.
// Copyright 2010 Kalidad Business Solutions. All rights reserved.
//
#import "PolygonShape.h"
@implementation PolygonShape
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
-(void)setNumberOfSides:(int)value {
if(value > minimumNumberOfSides && value < maximumNumberOfSides) {
numberOfSides = value;
} else {
NSLog(@"Invalid number of sides: %d. Must be in the range [%d-%d]", value, minimumNumberOfSides, maximumNumberOfSides);
}
}
-(void)setMinimumNumberOfSides:(int)value {
if(value < 2) {
NSLog(@"Minimum value must be greater than or equal to 2");
} else {
minimumNumberOfSides = value;
}
}
-(void)setMaximumNumberOfSides:(int)value {
if(value > 12) {
NSLog(@"Maximum value must be less than or equal to 12");
} else {
maximumNumberOfSides = value;
}
}
-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max {
minimumNumberOfSides = min;
maximumNumberOfSides = max;
numberOfSides = sides;
return self;
}
-(id)init {
if(self = [super init]) {
minimumNumberOfSides = 3;
maximumNumberOfSides = 10;
numberOfSides = 5;
}
return self;
}
-(float)angelInRadians {
return (M_PI * [self angelInDegrees]) / 180;
}
-(float)angelInDegrees {
float f = (180 * (numberOfSides - 2) / numberOfSides);
return f;
}
-(NSString *)name {
NSString *polyName = [NSString string];
switch (numberOfSides) {
case 2:
polyName = @"Digon";
break;
case 3:
polyName = @"Triangle";
break;
case 4:
polyName = @"Quadrilateral";
break;
case 5:
polyName = @"Pentagon";
break;
case 6:
polyName = @"Hexagon";
break;
case 7:
polyName = @"Heptagon";
break;
case 8:
polyName = @"Octagon";
break;
case 9:
polyName = @"Nonagon";
break;
case 10:
polyName = @"Decagon";
break;
case 11:
polyName = @"Hendecagon";
break;
case 12:
polyName = @"Dodecagon";
break;
default:
break;
}
return polyName;
}
-(NSString *)description {
NSString *d = [NSString stringWithFormat:@"Hello I am a %d-sided polygon (aka a %@) with angels of %f degrees (%f radians).", numberOfSides, [self name], [self angelInDegrees], [self angelInRadians]];
return d;
}
@end




1 comment
Post a Comment