Amazon.com Widgets Chapter 8 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 20, 2014, 10: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 2
Pages: [1]   Go Down
Print
Author Topic: Chapter 8 Exercise 2  (Read 574 times)
clouded
Full Member
***
Posts: 123






« on: May 16, 2012, 11:18:29 AM »

It's not seperated into separate files, but I think this is the idea:

Code: (Objective-C)
// Chapter 8 Exercise 2
// 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 (iOS uses a structure called CGRect for working with
// rectangles. All coordinates and sizes are expressed as floating point numbers
// when working with such rectangles). 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.

@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;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(int) area;
-(int) perimeter;
@end

@implementation Rectangle
{
    XYPoint *origin;
}
@synthesize width, height;
-(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;
}
-(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];
        [myRect setX: 100 andY: 200];
        [myRect setWidth: 5.5 andHeight: 8.1];
        NSLog (@"Origin at (%f, %f)", myRect.x, myRect.y);
        NSLog (@"Origin at (%f, %f)", myRect.width, myRect.height);
    }
    return 0;
}

Output:

Origin at (100.000000, 200.000000)
Origin at (5.500000, 8.100000)
Logged
m.savci
Newbie
*
Posts: 12






« Reply #1 on: July 22, 2012, 03:31:28 PM »

Hi, you create float width and height ,but when you calculate area and perimeter your return type is int. You should change your return type to float for these two functions or if you intentionally did this, you should use a type cast operator so that people could understand that you did this on purpose.
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.