Can someone PLEASE help me with my code?

Discussion in 'Computing, Science, and Technology' started by amandureja, Aug 28, 2010.

  1. amandureja

    amandureja New Member

    Joined:
    Apr 20, 2010
    Messages:
    306
    Likes Received:
    0
    Device:
    3G iPod touch
    Hi. I am currently learning objective c and am following a tutorial book. Anyways, an exercise in the book is to make and implement a "rectangle" class, but my code is not working for some reason.

    This is the code:

    [objc]
    //Rectangle Class

    #import <Foundation/Foundation.h>

    //---- @interface section ----

    @interface Rectangle: NSObject
    {
    int width;
    int height;
    }

    -(void) setWidth: (int) w;
    -(void) setHeight: (int) h;
    -(int) width;
    -(int) height;
    -(int) area;
    -(int) perimeter;
    @end

    //---- @implementation section ----

    @implementation Rectangle
    -(void) setWidth: (int) w;
    {
    width = w;
    }
    -(void) setHeight: (int) h;
    {
    height = h;
    }
    -(int) width
    {
    return width;
    }
    -(int) height
    {
    return height;
    }
    -(int) area: (int) a;
    {
    return width * height;
    }
    -(int) perimeter: (int) p;
    {
    return width * 2 + height * 2;
    }
    @end

    //---- program section ----

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

    [myRectangle setWidth: 6];
    [myRectangle setHeight: 3];
    [myRectangle width];
    [myRectangle height];
    [myRectangle area];
    [myRectangle perimeter];

    NSLog(@"The width of the rectangle is %i. The height is %i. The area is %i. The perimeter is %i",
    [myRectangle width],
    [myRectangle height],
    [myRectangle area],
    [myRectangle perimeter]);

    [myRectangle release];
    [pool drain];
    return 0;
    }
    [/objc]
    it compiles fine but then when i run it here is what i get in terminal:


    /Desktop/Exercise\ 7/build/Debug/Exercise\ 7 ; exit;
    2010-08-28 16:27:52.192 Exercise 7[994:10b] *** -[Rectangle area]: unrecognized selector sent to instance 0x1032f0
    2010-08-28 16:27:52.194 Exercise 7[994:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Rectangle area]: unrecognized selector sent to instance 0x1032f0'
    2010-08-28 16:27:52.195 Exercise 7[994:10b] Stack: (
    2476720107,
    2500103739,
    2476749290,
    2476742636,
    2476742834
    )
    Trace/BPT trap
    logout

    [Process completed]

    can someone please tell me what is missing in my code? because i really do not understand what the error is

    Please Register or Log in to view images

  2. Buzzard267

    Buzzard267 Member

    Joined:
    Jun 12, 2010
    Messages:
    727
    Likes Received:
    0
    Device:
    4G iPod touch
    Are you on Mac or PC?
  3. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Though you declared the method
    [objc]- (int)area;[/objc]
    you did not define it. Instead you mistakenly defined an undeclared method
    [objc]- (int)area

    Please Register or Log in to view images

    int)a[/objc]
    Your compiler should (must) have warned you that the implementation of class Rectangle was incomplete.

    You made the same mistake with the 'perimeter' method.

Share This Page