Amazon.com Widgets Chapter 8 Exercise 4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 11:19:50 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 8
| | | |-+  Chapter 8 Exercise 4
Pages: [1]   Go Down
Print
Author Topic: Chapter 8 Exercise 4  (Read 805 times)
clouded
Full Member
***
Posts: 123






« on: May 16, 2012, 01:44:51 PM »

I think this is where this exercise was going:

Code: (Objective-C)
// Chapter 8 Exercise 4
// Write a Rectangle method called translate: that takes an XYPoint object as its
// argument. Have it translate the rectangle’s origin by the specified vector.
// Note: Translation simply means moving the point from one place to another.

@interface XYPoint: NSObject
@property float x, y;
-(void) setX: (float) xVal andY: (float) yVal;
@end

@implementation XYPoint
@synthesize x, y;
-(void) setX: (float) xVal andY: (float) yVal
{
    x = xVal;
    y = yVal;
}
@end

@interface Rectangle: XYPoint
@property float width, height, tx, ty;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) translate: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(int) area;
-(int) perimeter;
@end

@implementation Rectangle
{
    XYPoint *origin;
}
@synthesize width, height, tx, ty;
-(void) setWidth: (float) w andHeight: (float) h
{
    width = w;
    height = h;
}
-(void) setOrigin: (XYPoint *) pt
{
    if (! origin)
        origin = [[XYPoint alloc] init];
    origin.x = pt.x;
    origin.y = pt.y;
}
-(void) translate: (XYPoint *) pt
{
    tx = origin.x + pt.x;
    ty = origin.y + pt.y;
}
-(int) area
{
    return width * height;
}
-(int) perimeter
{
    return (width + height) * 2;
}
-(XYPoint *) origin
{
    return origin;
}
@end

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        Rectangle *myRect = [[Rectangle alloc] init];
        XYPoint *origin = [[XYPoint alloc] init];
        XYPoint *vector = [[XYPoint alloc] init];
        [origin setX: 100 andY: 200];
        [myRect setOrigin: origin];
        [vector setX: 25 andY: 177.25];
        NSLog (@"Origin at (%f, %f)", myRect.origin.x, myRect.origin.y);
        NSLog (@"Vector at (%f, %f)", vector.x, vector.y);
        [myRect translate: vector];
        NSLog(@"Translate at (%f, %f)", myRect.tx , myRect.ty);
    }
    return 0;
}

Output:

Origin at (100.000000, 200.000000)
Vector at (25.000000, 177.250000)
Translate at (125.000000, 377.250000)
« Last Edit: May 19, 2012, 05:21:20 AM by clouded » Logged
krislwright
Newbie
*
Posts: 7


Email




« Reply #1 on: May 18, 2012, 05:32:08 AM »

Yeah, that makes sense. It would be nice if the author chimed in with the intent, as the idea is not very clear in the text.

Logged
tomtom
Newbie
*
Posts: 10






« Reply #2 on: May 18, 2012, 09:56:45 AM »

I've gone about this in pretty much the same way, but I didn't declare extra properties tx and ty like you did.  I would have thought that these are superfluous as the vector is already an XYPoint object and, as such, already holds those properties with the values assigned to them.  I've then used these values to change the original rectangle origin to a new one.

This is my translate method:
Code: (Objective-C)
-(void) translate: (XYPoint *) vector
{
    if (! origin)
        origin = [[XYPoint alloc] init];
   
    origin.x += vector.x;
    origin.y += vector.y;
   
}

And my test program:

Code: (Objective-C)
int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        Rectangle *myRect = [[Rectangle alloc] init];
        XYPoint *myRectOrigin = [[XYPoint alloc] init];
        XYPoint *myRectVector = [[XYPoint alloc] init];
       
        [myRectOrigin setX:100.25 andY:100.25];
        [myRect setOrigin:myRectOrigin];

        NSLog(@"myRect position: (%f, %f)", myRect.origin.x, myRect.origin.y);
       
        [myRectVector setX:250.50 andY:75.50];
        [myRect translate:myRectVector];
       
        NSLog(@"myRect vector: (%f, %f)", myRectVector.x, myRectVector.y);
        NSLog(@"myRect new position: (%f, %f)", myRect.origin.x, myRect.origin.y);
       
    }
    return 0;
}

Resulting in:
Quote
myRect position: (100.250000, 100.250000)
myRect vector: (250.500000, 75.500000)
myRect new position: (350.750000, 175.750000)
Logged
Gordio
Newbie
*
Posts: 12


Email




« Reply #3 on: July 12, 2012, 12:09:25 PM »

My solution was similar to tomtom, but I didn't think you need to reallocate memory, because you are not returning an instance.  It's a void method, not a return method.

In Rectangle:

-(void) translateXY:(XYPoint *)xy
{
    origin.x += xy.x;
    origin.y += xy.y;
}


In main, I added:

 XYPoint *moveRect = [[XYPoint alloc] init];
[…]
    [moveRect setX:100 andY: 100];
[…]
 [myRect translateXY:moveRect]; // these are the last two lines before the end
 NSLog(@"New rectangle origin is (%i, %i)", myRect.origin.x, myRect.origin.y);

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.