Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
June 26, 2017, 06:42:28 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 4 Exercise 7  (Read 7880 times)
Latias
Newbie
*
Posts: 2


Email
« on: August 14, 2015, 12:53:26 AM »

Code: (Objective-C)
#import <Foundation/Foundation.h>
@interface Rectangle: NSObject
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int) width;
-(int) height;
-(int) area;
-(int) preimeter;
@end

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

-(void) setWidth:(int)w
{
width = w;
}

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

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

-(int) preimeter {
return (width + height) * 2;
}
@end

int main(int argc, char *argv[]) {
@autoreleasepool {
Rectangle *myRectangle = [[Rectangle alloc]init];
[myRectangle setWidth: 2];
[myRectangle setHeight: 3];
NSLog(@"area = %d, preimeter = %d",[myRectangle area] ,[myRectangle preimeter]);
}
return 0;
}
Result:
2 warnings generated.
i don't know what is wrong in the code, help!
« Last Edit: August 14, 2015, 01:00:31 AM by Latias » Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #1 on: August 14, 2015, 06:33:42 AM »

In your @interface section you have defined two methods for returning the width & height, but you do not have any methods for them in the @implementation section.
Logged
Latias
Newbie
*
Posts: 2


Email
« Reply #2 on: August 14, 2015, 03:51:26 PM »

In your @interface section you have defined two methods for returning the width & height, but you do not have any methods for them in the @implementation section.
you mean i should delete them?
Code: (Objective-C)
-(void) setWidth:(int)w 
{
width = w;
}

-(void) setHeight:(int)h
{
height = h;
}
but it can't work.
Can you show me the correct code?
Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #3 on: August 14, 2015, 05:30:29 PM »

Those are the methods for setting the width & height. You should either delete these lines from the @interface section:
Code: (Objective-C)
-(int) width;
-(int) height;

or, add the code to implement those methods in the @implementation section:
Code: (Objective-C)
-(int) height
{
    return height;
}

-(int) width
{
    return width;
}
Logged
mitchb
Full Member
***
Posts: 152


« Reply #4 on: August 14, 2015, 08:16:53 PM »

You also have two variables that are unused, area and preimeter and can be deleted. Perimeter?

Code: (Objective-C)
NSLog(@"area = %d, preimeter = %d",[myRectangle area] ,[myRectangle preimeter]);

The code [myRectangle area] is calling the method area, not the variable area, and the same with [myRectangle preimeter] it's calling the method, not the variable.

Mitch

Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.