Amazon.com Widgets Exercise 7 in Detail
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 04:17:18 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 3
| | | |-+  Exercise 7 in Detail
Pages: [1]   Go Down
Print
Author Topic: Exercise 7 in Detail  (Read 1364 times)
JackRabbit
Jr. Member
**
Posts: 54



Email




« on: November 26, 2011, 10:31:24 PM »

I had trouble with Exercise 7.

The problem was the concept of getters and setters and their purpose.

At first I attempted to solve 7 with a program that was practically a copy of Program 3.2 on page 31 of the book. I saw a similar attempt on Chapter 3: Exercise program by sgg_admin posted on April 14, 2009.

Right after his/her post Steve replied with
Quote
It looks fine.  The difference is you have no getter methods for the x and y coordinates.  If you were actually implementing this class for use in programming applications, you'd probably want to implement them.

When I saw this I knew I had made the same mistake. I looked at other posts solving Exercise 7 and found the right way to do it. All this pointed to my lack of understanding of setters and getters. So to make sure I get it I am going to publish my code here and annotate it in detail. Please, those of you in the know, look at what I have done especially the annotation and make comments and corrections. If there are mistakes I will modify the annotated example until it is perfect in hopes that I and others will fully understand this concept.

//
//  main.m
//  Exercise 3.7 print the value of a Cartesian coordinate in the console.
//


#import <Foundation/Foundation.h>



//---- @inteface section ----

@interface CartCoordinate :  NSObject
{
    int getXcoordinate;                         declared getter variable
    int getYcoordinate;                         declared getter variable
}

-(void) setXcoordinate: (int) x;                declared setter method
-(void) setYcoordinate: (int) y;                declared setter method
-(int) getXcoordinate;                          declared getter method
-(int) getYcoordinate;                          declared getter method

@end



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

@implementation CartCoordinate


-(void) setXcoordinate: (int) x

{
    getXcoordinate = x;                         code for setter method
}


-(void) setYcoordinate: (int) y

{
    getYcoordinate = y;                         code for setter method
}


-(int) getXcoordinate

{
     return getXcoordinate;                     code for getter method
}


-(int) getYcoordinate

{
    return getYcoordinate;                      code for getter method
}

@end


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

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

    @autoreleasepool
    {
        CartCoordinate *xyCoord = [[CartCoordinate alloc] init];
    
        
        
        // Set Cartesian Coordinate to (-5,3)
        
        [xyCoord setXcoordinate: -5];
        [xyCoord setYcoordinate:  3];
        
       // Display Cartesian Coordinate
       
        NSLog(@"The Cartesian Coordinate is (%i,%i)."), [xyCoord getXcoordinate], [xyCoord getYcoordinate]);
    }
    
    return 0;
}

Once a class is created, Objective-C has this feature of “encapsulation”. Through encapsulation, the structure (variables and methods) becomes an opaque token. Another programmer never has to look inside the object to use it. Encapsulation keeps the integrity of the object intact. But sometimes you have to set and retrieve the variables to and from an object. Setters and getters come into play when you want to access these variables from outside that class.

So back to Exercise 7, we created a CartCoodinate class whose parent class is NSObject.

@interface CartCoordinate :  NSObject

Then we declare the getter variables and the setter and getter methods. All this is done in the @interface section

In the @implementation section, we create the code for the setter and getter methods.

So now we have a new object “CartCoordinate” with it’s variables and methods declared in the @interface section and the methods are defined in the @implementation section… and this is where I get shaky….

We use getters and setters to access our instance variables cleanly from “outside the class”. Is “outside the class” *xyCoord? The book states that “The asterisk (*) in front of (xyCoord) in its declaration says that xyCoord is actually a reference (or pointer) to a CartCoordinate object.” This makes xyCoord a new object. Right?

So now from xyCoord…
we use setters

            [xyCoord setXcoordinate: -5];
            [xyCoord setYcoordinate:  3];

to set the variables found inside CartCoordinate…


and getters

[xyCoord getXcoordinate], [xyCoord getYcoordinate]

to access our instance variables found inside CartCoordiante.


Have I got this right? I hope so!


JR
« Last Edit: November 27, 2011, 11:35:41 PM by JackRabbit » Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




« Reply #1 on: November 27, 2011, 09:01:21 AM »

JR,

To save you time using the "colour" tags, simply highlight your code and click on the "code" button (bottom, third from right - looks like a page with a # in it).

Nick
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




« Reply #2 on: November 27, 2011, 09:53:54 AM »

Thank you Nick, I'll do just that in the future.

And while we are on the topic of color coded fonts in Xcode...

When you look at my program in Xcode:


in the @interface section - getXcoordinate, getYcoordinate & setXcoordinate, setYcoordinate - the fonts are all black.

{
    int getXcoordinate;
    int getYcoordinate;
}

-(void) setXcoordinate: (int) x;
-(void) setYcoordinate: (int) y;
-(int) getXcoordinate;
-(int) getYcoordinate;



in the @implement section getXcoordinate getYcoordinate between the curly backers are grey

-(void) setXcoordinate: (int) x
{
    getXcoordinate = x;
}


-(void) setYcoordinate: (int) y
{
    getYcoordinate = y;
}


-(int) getXcoordinate
{
    return getXcoordinate;
}


-(int) getYcoordinate
{
    return getYcoordinate;
}




in the program section getXcoordinate and getYcoordinate are a very dark grey, almost black

The change of color means a change of role in the program. I went into Xcode and went into preferences and looked at the color coding but couldn't figure it out.

Does anybody know what is happening here?


JR
« Last Edit: November 28, 2011, 05:21:28 AM by JackRabbit » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #3 on: December 06, 2011, 05:45:26 PM »

JR,

Yes, from "outside" the class, you use the setters and getter methods to set and retrieve, repsectively, the values of your instance variables.

Cheers,

Steve
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




« Reply #4 on: December 14, 2011, 07:06:33 AM »

Thank you Steve for your reply on this. I thought I had it but I was unsure. Now I feel better.

JR
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.