Amazon.com Widgets Exercise 11.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 26, 2014, 10:16:25 AM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 11
| | | |-+ Exercise 11.5
Pages: [1] Go Down
Print
Author Topic: Exercise 11.5 (Read 1060 times)
tknp
Newbie
*
Posts: 23






on: January 17, 2012, 08:43:22 PM

I believe this is the way it was intended to be done.

Rectangle.m
Code: (Objective-C)
#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

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

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

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

@end

Square.m
Code: (Objective-C)
#import "Square.h"
#import "Rectangle.h"

@implementation Square

Rectangle *rect;

@synthesize side;

-(Square *) initWithSide: (int) s
{
self = [super init];

if (self)
rect = [[Rectangle alloc] init];
[rect setWidth: s andHeight: s];
[self setSide: s];

return self;
}
-(void) setSide: (int) s
{
side = s;
}

-(int) area
{
return [rect area];
}
-(int) perimeter
{
return [rect perimeter];
}

@end

main.m
Code: (Objective-C)
#import "Square.h"

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

Square *a = [[Square alloc] initWithSide: 10];

NSLog(@"Side is: %i", a.side);
NSLog(@"Area is: %i", a.area);
NSLog(@"Perimeter is: %i", a.perimeter);

    [pool drain];
    return 0;
}
Logged
BombermanX
Newbie
*
Posts: 4


Email




Reply #1 on: February 19, 2012, 10:11:31 PM

Hi tknp,
I noticed you have a 'side' iVar as well as a separate 'rect.width' and 'rect.height'. This means if you change 'side' iVar again, the area and perimeter calculations for your square won't match up.
Logged
m.sniper
Newbie
*
Posts: 5






Reply #2 on: March 03, 2012, 06:19:31 PM

I have the followings:

Square.m
Code: (Objective-C)
#import "Square.h"

@implementation Square

-(Square *) initWithSide: (int) s
{
    self = [self init];
    if (self)
    {
        rect = [[Rectangle alloc] init ];
        [self setSide: s];
    }
        return self;
}

-(void) setSide: (int) s{
    [rect setWidth: s andHeight:s];
}
-(int) area
{
    return [rect area];
}
-(int) side
{
    return [rect width];
}
-(int) perimeter
{
    return [rect perimeter];
}

@end

Logged
elkie13
Newbie
*
Posts: 17






Reply #3 on: March 09, 2012, 11:53:56 AM

Well, here's how I got it to work.  I don't really understand this question.  I guess we were supposed to use rect as the variable.  All the methods for rect will work with square,  and if you have them written not to change the original, I don't really see how this helps.  I think I must really be missing something from this chapter.


#import "Square.h"

@implementation Square


{

    Rectangle *rect;

}
-(Square *) initWithSide:(int)s
{
    self= [self init];
    if (self)
    {
        rect = [[Rectangle alloc]init];
        [rect setwidth:s andHeight:s];
        [self setside:s];
       
    }
    return self;

}
-(void) setside:(int)s
{
    [rect setwidth:s andHeight:s];
}

     
-(int) side
{
    return [rect width];
}
-(int) area;
{
    return [rect area];
}
-(int) perimeter
{
    return [rect perimeter];
}

@end
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.