Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Simple Rectangle Class Problem

The code below correctly computes the result of the area of the rectangle but then gets each of the following values incorrect, can anyone offer some advice?:-

2012-06-10 10:22:57.477 test23[2666:403] The area is 100
2012-06-10 10:22:57.479 test23[2666:403] The perimeter is 220
2012-06-10 10:22:57.480 test23[2666:403] The height is 200
2012-06-10 10:22:57.480 test23[2666:403] The width is 20

#import

@interface Rectangle : NSObject
-(void) setHeight: (int) H;
-(void) setWidth: (int) W;
-(int) Width;
-(int) Area;
-(int) Height;
-(int) Perimeter;

@end

@implementation Rectangle
{
int width;
int height;
int area;
int perimeter;
}

-(void) setHeight:(int)H
{
height = H;
}

-(void) setWidth:(int)W
{
width = W;
}
-(int) Width
{
return width;
}
-(int) Height
{
return height;
}

-(int) Area
{
area = (height *= width);
return area;
return 0;
}

-(int) Perimeter
{
perimeter = (height *= 2) + (width *= 2);
return perimeter;

}

@end

int main(int argc, const char * argv[])
{

@autoreleasepool {

Rectangle *myRectangle = [[Rectangle alloc]init];

[myRectangle setWidth:10];
[myRectangle setHeight:10];

NSLog(@"The area is %i", [myRectangle Area]);
NSLog(@"The perimeter is %i", [myRectangle Perimeter]);
NSLog(@"The height is %i", [myRectangle Height]);
NSLog(@"The width is %i", [myRectangle Width]);
}
return 0;
}
Post edited by Thrivius on
Sign In or Register to comment.