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
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.//
// Exercise 3.7 print the value of a Cartesian coordinate in the console.
//---- @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
//---- @implementation section ----
-(void) setXcoordinate: (int) x
getXcoordinate = x; code for setter method
-(void) setYcoordinate: (int) y
getYcoordinate = y; code for setter method
return getXcoordinate; code for getter method
return getYcoordinate; code for getter method
//---- program section ----
int main (int argc, const char * argv)
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]);
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