Amazon.com Widgets Chapter 10 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 01:23:36 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 10
| | | |-+  Chapter 10 Exercise 2
Pages: [1]   Go Down
Print
Author Topic: Chapter 10 Exercise 2  (Read 446 times)
clouded
Full Member
***
Posts: 123






« on: May 31, 2012, 08:24:25 AM »

Here's my take on this one, notice the self statements in the methods:

Code: (Objective-C)
// Chapter 10 Exercise 2
// Given that you label the method developed in exercise 1 the designated initializer
// for the Rectangle class, and based on the Square and Rectangle class definitions
// from Chapter 8, add an initializer method to the Square class according to the
// following declaration:
//     -(id) initWithSide: (int) side;

//  main.m

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Square.h"

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        Rectangle *rect1, *rect2;
        
        rect1 = [[Rectangle alloc] init];
        NSLog (@"Rectangle: w = %i, h = %i", rect1.width, rect1.height);
        NSLog (@"Area = %i, Perimeter = %i", [rect1 area], [rect1 perimeter]);
        NSLog (@" ");
        
        [rect1 setWidth: 5 andHeight: 8];
        NSLog (@"Rectangle: w = %i, h = %i", rect1.width, rect1.height);
        NSLog (@"Area = %i, Perimeter = %i", [rect1 area], [rect1 perimeter]);
        NSLog (@" ");
        
        rect2 = [[Rectangle alloc] initWithWidth: 6 andHeight: 9];
        NSLog (@"Rectangle: w = %i, h = %i", rect2.width, rect2.height);
        NSLog (@"Area = %i, Perimeter = %i", [rect2 area], [rect2 perimeter]);
        NSLog (@" ");

        Square *mySquare = [[Square alloc] initWithSide: 5]; // <-- replaced " init]" with " initWithSide: 5]"
        // [mySquare setSide: 5]; // <-- Commented to omit
        NSLog (@"Square s = %i", [mySquare side]);
        NSLog (@"Area = %i, Perimeter = %i", [mySquare area], [mySquare perimeter]);
    }
    return 0;
}
Code: (Objective-C)
//  Square.h

#import "Rectangle.h"

@interface Square : Rectangle

-(id) initWithSide: (int) side;
-(void) setSide: (int) s;
-(int) side;
@end
Code: (Objective-C)
//  Square.m

#import "Square.h"

@implementation Square

-(id) initWithSide: (int) side
{
    self = [super init];
    if (self)
        self = [self initWithWidth: side andHeight: side];
    return self;
}
-(void) setSide: (int) s
{
    [self setWidth: s andHeight: s];
}
-(int) side
{
    return self.width;
}
@end
Code: (Objective-C)
//  Rectangle.h

#import <Foundation/Foundation.h>

@interface Rectangle: NSObject
@property int width, height;

-(id) init;
-(id) initWithWidth: (int) w andHeight: (int) h;
-(void) setWidth: (int) w andHeight: (int) h;
-(int) area;
-(int) perimeter;
@end
Code: (Objective-C)
//  Rectangle.m

#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;

-(id) init
{
    self = [super init];
    if (self)
        [self setWidth: 1 andHeight: 1];
    return self;
}
-(id) initWithWidth: (int) w andHeight: (int) h
{
    self = [super init];
    if (self)
        [self setWidth: w andHeight: h];
    return self;
}
-(void) setWidth: (int) w andHeight: (int) h
{
    width = w;
    height = h;
}
-(int) area
{
    return width * height;
}
-(int) perimeter
{
    return (width + height) * 2;
}
@end

Output:

Rectangle: w = 1, h = 1
Area = 1, Perimeter = 4

Rectangle: w = 5, h = 8
Area = 40, Perimeter = 26
 
Rectangle: w = 6, h = 9
Area = 54, Perimeter = 30

Square s = 5
Area = 25, Perimeter = 20
« Last Edit: May 31, 2012, 08:42:52 AM by clouded » Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #1 on: August 21, 2012, 04:38:39 AM »

Code: (Objective-C)
-(id) initWithSide:(int)side
{
    self = [super init];
   
    if (self)
        [self setSide:side];
   
    return self;
}
Code: (Objective-C)
        Square *newSquare = [[Square alloc] initWithSide:15];
        NSLog(@"newSquare side = %i", [newSquare side]);
2012-08-21 12:36:45.132 Rectangle[776:303] newSquare side = 15
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.