Amazon.com Widgets Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 29, 2014, 05:14:33 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 18
| | | |-+ Exercise 2
Pages: [1] Go Down
Print
Author Topic: Exercise 2 (Read 662 times)
Brabbeldas
Newbie
*
Posts: 10






on: January 26, 2012, 06:38:18 AM

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

@interface XYPoint : NSObject <NSCopying>
{
    int x;
    int y;
}

@property int x, y;

-(void) setX: (int) xVal andY: (int) yVal;

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

@end

The related method from the XYPoint implementation file:
Code: (Objective-C)
-(id) copyWithZone:(NSZone *)zone
{
    XYPoint *newXYPoint = [[XYPoint allocWithZone:zone] init];
   
    [newXYPoint setX:x andY:y];
   
    return  newXYPoint;
}


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

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

@property int width, height;

-(id) initWidth: (int) w andHeight: (int) h;

-(void) setOrigin: (XYPoint *) pt;
-(XYPoint *) origin;
-(int) area;
-(int) perimeter;
-(void) setWidth:(int)w andHeight:(int) h;

-(void) dealloc;

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

The related method from the Rectangle implementation file:
Code: (Objective-C)
-(id) copyWithZone:(NSZone *)zone
{
    Rectangle *newRectangle = [[Rectangle allocWithZone:zone] initWidth:width andHeight:height];
   
    [newRectangle setOrigin: [origin copy]];
   
    return  newRectangle;
}


The main.m file for testing:
#import <Foundation/Foundation.h>
#import "Rectangle.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
       
        // insert code here...
        XYPoint *origin1 = [[XYPoint alloc] init];
        [origin1 setX:10 andY:10];
       
        Rectangle *rectangle1 = [[Rectangle alloc] initWidth:10 andHeight:10];
        [rectangle1 setOrigin:origin1];
       
       
        // Create copy //
        Rectangle *rectangle2 = [rectangle1 copy];
        [[rectangle2 origin] setX:100 andY:100];
        [rectangle2 setWidth:100 andHeight:100];
       
       
        // Display both rectangles //
        NSLog(@"Rectangle1, width: %i, height: %i", [rectangle1 width], [rectangle1 height]);
        NSLog(@"Has origin, x: %i, y: %i", [[rectangle1 origin] x], [[rectangle1 origin] y]);
       
        NSLog(@"Rectangle2, width: %i, height: %i", [rectangle2 width], [rectangle2 height]);
        NSLog(@"Has origin, x: %i, y: %i", [[rectangle2 origin] x], [[rectangle2 origin] y]);       
    }
    return 0;
}


A) Does it make sense to implement both mutable and immutable copies for these classes:
It does not, as both width and height and both x and y are not objects.
Maybe somebody else can give a better explanation?

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.