Amazon.com Widgets Attempted answer to exercise 18.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 21, 2014, 01:13:20 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
| |-+ Answers to Exercises
| | |-+ Chapter 18
| | | |-+ Attempted answer to exercise 18.2
Pages: [1] Go Down
Print
Author Topic: Attempted answer to exercise 18.2 (Read 2077 times)
mdeh
Full Member
***
Posts: 166






on: March 01, 2009, 05:37:23 PM

Steve, et al.
Your criticism is needed!

XYPoint.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface XYPoint : NSObject <NSCopying> {

int x;
int y;

}

@property  int x, y;

-(id) initWithPt: (int) ptX andPtY: (int) ptY;

-(void) dealloc;

@end

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


@implementation XYPoint
@synthesize x, y;

-(id) initWithPt: (int) ptX andPtY: (int) ptY
{
self = [super init];

if (self)
{
[self setX: ptX];
[self setY: ptY];
}

return self;
}


-(void) dealloc
{
[super dealloc];
}

-(id) copyWithZone: (NSZone*) zone
{

XYPoint *newPt = [[ XYPoint allocWithZone:zone] init];
[newPt initWithPt: self.x andPtY: self.y];
return newPt;

}

@end

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

@interface Rectangle : NSObject <NSCopying> {
int width;
int height;
XYPoint *origin;
}

@property int width, height;
@property (copy, nonatomic)  XYPoint *origin;

-(void) setWidth: (int) w andHeight: (int) h;

-(int) area;
-(int) perimeter;

-(void) print;

-(void) dealloc;

@end

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



@implementation Rectangle

@synthesize width, height, origin;

-(void) setWidth: (int) w andHeight: (int) h
{
[self setWidth: w];
[self setHeight: h];
}


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

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

-(void) print
{
NSLog(@"\nWidth: %i\nHeight: %i\nOrgin: x: %i y %i", self.width, self.height, self.origin.x, self.origin.y);
}

-(id) copyWithZone: (NSZone *) zone
{
Rectangle *newRect = [ [ Rectangle alloc] init];
[newRect setWidth: self.width andHeight: self.height];
[newRect setOrigin: [origin copy]];
return newRect;
}


-(void) dealloc
{
[origin release];
[super dealloc];
}

@end

test Program  main.m
Code: (Objective-C)
#import "Rectangle.h"
#import "XYPoint.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
XYPoint *pt = [[XYPoint alloc] initWithPt:45 andPtY: 78];
Rectangle *myRect = [[Rectangle alloc] init], *copiedRect;

[myRect setWidth: 100 andHeight: 34];
[myRect setOrigin: pt];
NSLog(@"Print all values assigned to Rectangle");
[myRect print];
NSLog(@"Copy rectangle");
copiedRect = [myRect copy];
NSLog(@"Print all values assigned to copy");
[copiedRect print];


[copiedRect release];
[myRect release];
    [pool drain];
    return 0;
}

As to the question of mutable/immutable copies...hmm!  Smiley
The values one is working with are not really foundation classes, so not sure how mutability would affect these?
Last Edit: March 01, 2009, 05:40:56 PM by mdeh Logged
esc
Global Moderator
Full Member
*****
Posts: 230






Reply #1 on: March 01, 2009, 06:06:37 PM

Hi Michael,

I did it pretty much the same way.  I think the main point was to use the "copy" implemented for the origin inside Rectangle's copyWithZone:

Code: (Objective-C)
-(id) copyWithZone: (NSZone *) zone
{
Rectangle *newRect = [[[self class] allocWithZone: zone] init];
[newRect setWidth: width andHeight: height];

newRect.origin = [origin copy];

return newRect;
}

A couple of (minor) differences between our programs:
1.  use of "self class"
2.  I did not use self.<variable> to ensure access via the synthesized methods (I did this exercise before the discussion on "self").
3.  use of dot operator

Logged
mdeh
Full Member
***
Posts: 166






Reply #2 on: March 01, 2009, 06:20:51 PM

Hi Michael,

I did it pretty much the same way.  I think the main point was to use the "copy" implemented for the origin inside Rectangle's copyWithZone:

Code: (Objective-C)
-(id) copyWithZone: (NSZone *) zone
{
Rectangle *newRect = [[[self class] allocWithZone: zone] init];
[newRect setWidth: width andHeight: height];

newRect.origin = [origin copy];

return newRect;
}

A couple of (minor) differences between our programs:
1.  use of "self class"


Never did get a really good reason for using self...but if you are going to subclass your class ..is that what you are doing it for.

Quote
2.  I did not use self.<variable> to ensure access via the synthesized methods (I did this exercise before the discussion on "self").
Hmmm....?. I did not think there was a difference between self and dot. I thought the difference was between the bare variable, like x as in

x = something /*WRONG?  plain assignment */

versus

setX:something or self.x = something

Thanks for the input.
Logged
esc
Global Moderator
Full Member
*****
Posts: 230






Reply #3 on: March 01, 2009, 07:17:15 PM

Quote
Never did get a really good reason for using self...but if you are going to subclass your class ..is that what you are doing it for.

Yes, in preparation for subclass -- but not for this exercise.

Quote
Hmmm....?. I did not think there was a difference between self and dot. I thought the difference was between the bare variable, like x as in

x = something /*WRONG?  plain assignment */

versus

setX:something or self.x = something

Yes, self.x and setX have the same effect.  Either way works but not the plain assignment as you marked WRONG above.

The "self" difference that I was referring was:
Code: (Objective-C)
[newRect setWidth: self.width andHeight: self.height];
versus
Code: (Objective-C)
[newRect setWidth: width andHeight: height];

Thanks!
Logged
mdeh
Full Member
***
Posts: 166






Reply #4 on: March 01, 2009, 07:25:01 PM

The "self" difference that I was referring was:
Code: (Objective-C)
[newRect setWidth: self.width andHeight: self.height];
versus
Code: (Objective-C)
[newRect setWidth: width andHeight: height];

Thanks!


OH...**that** self. Same reason as we said. I wanted to make sure that the ivar was accessed by the getter and not directly. Not sure if it makes any difference, except getting into the habit of always using the accessors.

Ps..thank you for looking at it. Makes these exercises more fun!
Logged
goofy
Newbie
*
Posts: 12


Email




Reply #5 on: March 24, 2012, 10:22:03 AM

Hi Michael,

Sorry for resurrecting such an old topic but I am only just working on this chapter and had just found a chance to see what you have done in exercise 18.2.

I have a concern about the codpiece below:

Code: (Objective-C)
-(id) copyWithZone: (NSZone *) zone  
{  
    Rectangle *newRect = [ [ Rectangle alloc] init];  
    [newRect setWidth: self.width andHeight: self.height];  
    [newRect setOrigin: [origin copy]];  
    return newRect;  
}  

Here, in copying the origin instance, you are using both the synthesized setter for the origin instance variable and the copy method for the XYPoint class. However, since the synthesized property for origin is defined as "copy", and "non atomic", then the setter, "setOrigin" will also invoke the copy method for XYPoint. That is, you are first creating a copy by:

Code: (Objective-C)
[origin copy]

Then, you are passing this copy to the setOrigin method and it creates a second copy from this copied XYPoint object and assigns it to the instance variable "origin" of the new Rectangle object.

So I am afraid we'll have a memory leakage there.

This is just what I deduce depending on my understanding up to now. Hope I am not mistaken and not bothering you with some useless stuff.

By the way, almost every post of you both (mdeh and esc) have been of great help for me, throughout my study and let me thank you for all this valuable stuff that you have put out there.

  
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.