Amazon.com Widgets Exercise #7
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 12:24:14 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Webcast Series Part I, Sept. 1 - 24
| | |-+  Chapter 4, Exercise 7
| | | |-+  Exercise #7
Pages: [1]   Go Down
Print
Author Topic: Exercise #7  (Read 621 times)
jonthornham
Full Member
***
Posts: 169



WWW Email




« on: September 05, 2009, 11:51:56 AM »

Here is my answer to Exercise #7

Code: (Objective-C)
// Rectangle Program 

#import <Foundation/Foundation.h>

// @interface Section
@interface Rectangle: NSObject
{
int width;
int height;
int area;
int perimeter;
}

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

// @implementation Section
@implementation Rectangle
-(void) setWidth: (int) w
{
width = w;
}

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

-(int) width
{
return width;
}

-(int) height
{
return height;
}

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

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

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

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

[rectangle1 setWidth: 18];
[rectangle1 setHeight: 36];

NSLog(@"The width of the rectangle is %i", [rectangle1 width]);
NSLog(@"The height of the rectangle is %i", [rectangle1 height]);
NSLog(@"The area of the rectangle is %i", [rectangle1 area]);
    NSLog(@"The perimeter of the rectangle is %i", [rectangle1 perimeter]);

[rectangle1 release];
    [pool drain];
    return 0;
}

Take care,

Jon
Logged

Jon Thornham
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: September 08, 2009, 08:37:20 AM »

Since the area and perimeter are calculated each time the respected method is called, there's no purpose to store them as instance variables in your objects.   

Cheers,

Steve Kochan
Logged
Pages: [1]   Go Up
Print
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.