Amazon.com Widgets 8.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 30, 2014, 02:04:24 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 8
| | | |-+ 8.2
Pages: [1] Go Down
Print
Author Topic: 8.2 (Read 1108 times)
sir
Full Member
***
Posts: 118


Email




8.2
on: March 14, 2009, 09:27:26 PM

main.m
Code: (Objective-C)
/*
When dealing with higher-resolution devices,
you might need to use a coordinate system that enables you
to specify points as floating-point values instead of as simple integers.
Modify the XYPoint and Rectangle classes from this chapter to deal with
floating-point numbers. The rectangle's width, height, area, and perimeter
should all work with floating-point numbers as well.
*/
#import "XYPoint.h"
#import "Rectangle.h"


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

Rectangle *myRect = [Rectangle new];
XYPoint *myPoint = [XYPoint new];

[myPoint setX: 10.5 andY: 12.4];

[myRect setWidth: 12.3 andHeight: 5.4];

myRect.origin = myPoint;

NSLog(@"Rectangle w = %f, h = %f", myRect.width, myRect.height);
NSLog(@"Origin at: (%f, %f)", myRect.origin.x, myRect.origin.y);
NSLog(@"Area = %f", [myRect area]);
NSLog(@"Perimeter = %f", [myRect perimeter]);

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

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


@interface XYPoint : NSObject {
double x, y;
}
@property double x, y;

-(void) setX: (double) xVal andY: (double) yVal;
@end
XYPoint.m
Code: (Objective-C)
#import "XYPoint.h"


@implementation XYPoint

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

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


@interface Rectangle : NSObject {
double width, height;
XYPoint *origin;
}

@property double width, height;

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

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


@implementation Rectangle

@synthesize width, height;

-(XYPoint *) origin {
return origin;
}

-(void) setOrigin: (XYPoint *) pt {
origin = pt;
}

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

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

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

@end


Logged
nhohmann
Global Moderator
Jr. Member
*****
Posts: 57







Reply #1 on: July 13, 2009, 05:48:20 AM

I simply changed the integer data type, int, to a float data type in order to accommodate floating-point values.  I then changed the coordinates to numbers that might reflect those of a higher-resolution device.

Console output:
Code: (Objective-C)
[Session started at 2009-07-13 14:41:44 +0200.]
2009-07-13 14:41:44.397 Prog ex8.2[315:10b] Rectangle: w = 5.000000, h = 8.000000
2009-07-13 14:41:44.400 Prog ex8.2[315:10b] Origin at (100.449997, 198.800003)
2009-07-13 14:41:44.400 Prog ex8.2[315:10b] Area = 40.000000, Perimeter = 26.000000

The Debugger has exited with status 0.

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

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

Rectangle *myRect = [[Rectangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];

[myPoint setX: 100.45 andY: 198.80];

[myRect setWidth: 5 andHeight: 8];

myRect.origin = myPoint; // [myRect setOrigin: myPoint];

NSLog (@"Rectangle: w = %f, h = %f", myRect.width, myRect.height); // equal to [myRect width], [myRect height]
NSLog (@"Origin at (%f, %f)", myRect.origin.x, myRect.origin.y);
NSLog (@"Area = %f, Perimeter = %f", [myRect area], [myRect perimeter]);


[[myRect origin] release];
[myRect release];

[myPoint release];

    [pool drain];
    return 0;
}

Rectangle.h
Code: (Objective-C)
//
//  Rectangle.h
//  Prog ex8.2
//
//  Created by Neil Hohmann on 7/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "XYPoint.h"

@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
}

@property float width, height;

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

@end

Rectangle.m
Code: (Objective-C)
//
//  Rectangle.m
//  Prog ex8.2
//
//  Created by Neil Hohmann on 7/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Rectangle.h"


@implementation Rectangle

@synthesize width, height;

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

-(void) setOrigin: (XYPoint *) pt
{
origin = [[XYPoint alloc] init];

[origin setX: pt.x andY: pt.y];
}

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

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

-(XYPoint *) origin
{
return origin;
}

@end

XYPoint.h
Code: (Objective-C)
//
//  Rectangle.h
//  Prog ex8.2
//
//  Created by Neil Hohmann on 7/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "XYPoint.h"

@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
}

@property float width, height;

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

@end

XYPoint.m
Code: (Objective-C)
//
//  XYPoint.m
//  Prog ex8.2
//
//  Created by Neil Hohmann on 7/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void) setX: (float) xVal andY: (float) yVal
{
x = xVal;
y = yVal;
}

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