Amazon.com Widgets Exercise 7 Chapter 4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 04:41:33 AM
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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 4
| | | |-+  Exercise 7 Chapter 4
Pages: [1]   Go Down
Print
Author Topic: Exercise 7 Chapter 4  (Read 443 times)
alexsom
Jr. Member
**
Posts: 63



Email




« on: January 16, 2012, 12:31:55 PM »

Here is my take on Exercise 7 Chapter 4.


#import <Foundation/Foundation.h>

//......interface section...........
@interface Rectangle:NSObject
{
    int width;
    int height;
}
//.....methods for width and height.....

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

@end

//.....implementation section.....
@implementation Rectangle: NSObject

-(void)setWidth: (int) w;
{
   width=w;
}
-(void)setHeight:(int) h;
{
    height=h;
}
-(int)width
{
    return width;
}
-(int)height;
{
    return height;
}
-(int) area;
{
    return width*height;
}
-(int) perimeter;
{
    return 2*width+2*height;
}

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

   
    @autoreleasepool
   
    {
    Rectangle *myRectangle=[[Rectangle alloc] init];
       
    [myRectangle setWidth: 15];
    NSLog(@"Set the width to %i", [myRectangle width]);
       
    [myRectangle setHeight:20];
    NSLog(@"Set the height to %i",[myRectangle height]);   
       
    NSLog(@"Area of the rectangular is %i",[myRectangle area]);   
       
    NSLog(@"Perimeter of the rectangle is %i", [myRectangle perimeter]);   
    }
    return 0;
}

Output Window:

Set the width to 15
Set the height to 20
Area of the rectangular is 300
Perimeter of the rectangle is 70
 
All the best.
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.