Welcome, Guest. Please login or register.
Did you miss your activation email?
September 09, 2010, 09:36:13 AM
Home Help Search chat Login Register   
News:   If you like the book and forum please  write a review on Amazon   Recorded lessons available for iPad viewing and download....in process! Consulting and onsite training available!   If you're running Xcode 3.2 (or later), see this post   Follow me on Twitter  @skochan  Forum Guests: register to download (or see) attachments.    Join our mailing list.


+  Official Forum for Programming in Objective-C 2.0 (the iPhone Programming Language) - Stephen Kochan
|-+  Introduction to iPhone Programming Webcast Series (March 2 - May 27, 2010)
| |-+  Part I - Programming in Objective-C 2.0
| | |-+  Chapter 4, Exercise 7
| | | |-+  Exercise 7, Chapter 4
« previous next »
Pages: [1] Go Down Print
Author Topic: Exercise 7, Chapter 4  (Read 129 times)
MarkReid
Full Member
***
Posts: 175


« on: March 11, 2010, 04:51:55 AM »

Sorry it's a little late but took a little longer than expected to get the free time to complete the assignments this last week.

Was tempted to add a initWithWidth: andHeight: method but kept it simple so it would be easier for others to compare notes.  Smiley

main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Rectangle.h"

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

Rectangle *myRect = [[Rectangle alloc] init];

[myRect setWidth: 4];
[myRect setHeight: 2];

NSLog(@"Your rectangle is %i wide and %i high.", [myRect width], [myRect height]);

NSLog(@"The area of your rectangle is %i \n and the perimeter of your rectangle is %i.", [myRect area], [myRect perimeter]);

[myRect release];

    [pool drain];
    return 0;
}

Rectangle.h
Code: (Objective-C)
//  Rectangle.h

#import <Cocoa/Cocoa.h>


@interface Rectangle : NSObject {
int width;
int height;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int) width;
-(int) height;
-(int) area;
-(int) perimeter;

@end

Rectangle.m
Code: (Objective-C)
//  Rectangle.m

#import "Rectangle.h"


@implementation Rectangle

-(void) setWidth: (int) w
{
width = w;
}

-(void) setHeight: (int) h
{
height = h;
}

-(int) width
{
return width;
}

-(int) height
{
return height;
}

-(int) area
{
return width * height;
}

-(int) perimeter
{
return width * 2 + height * 2;
}

@end
Logged
Pages: [1] Go Up Print 
« previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.