Widgets Chapter 8 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 13, 2015, 08:17:31 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
Author Topic: Chapter 8 Exercise 2  (Read 731 times)
Full Member
Posts: 123

« on: May 16, 2012, 12:18:29 PM »

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;

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

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

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

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;


Origin at (100.000000, 200.000000)
Origin at (5.500000, 8.100000)
Posts: 12

« Reply #1 on: July 22, 2012, 04: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.
Pages: [1]   Go Up
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 All rights reserved.