Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
January 23, 2018, 09:05:42 AM *
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 [2] 3 4 ... 6   Go Down
  Print  
Author Topic: Chapter 3: Exercice program  (Read 37789 times)
Freerider
Newbie
*
Posts: 16


« Reply #15 on: April 16, 2009, 01:57:57 AM »

OK I think I understand.

On page 44 you say "an instance method can always directly access its instance variables"
Is that why the program works?

If I wanted to access the variables outside of main, I would need to use the getter method.
Is that right?

Sorry if I'm being dim!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #16 on: April 16, 2009, 04:29:17 AM »

Yes, that's right.  Because the methods can directly access the instance variables you didn't need to write the getter methods.

Cheers,

Steve Kochan
Logged
Jakers
Newbie
*
Posts: 2



« Reply #17 on: April 16, 2009, 05:26:13 PM »

Code: (Objective-C)
#import <Foundation/Foundation.h>



//EXERCISE CHAPTER 4, Q#7

#import <Foundation/Foundation.h>

// ---- @interface section ----
@interface XYPoint : NSObject
{
int xCord;
int yCord;
}

- (void) print;
- (void) setXCord: (int) x;
- (void) setYCord: (int) y;
// ---- Getters Rule The Divine ----
- (int) xCord;
- (int) yCord;

@end

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

- (void) print
{
NSLog(@"%i by %i", xCord, yCord);
}

- (void) setXCord: (int) x
{
xCord = x;
}

- (void) setYCord: (int) y
{
yCord = y;
}

- (int) xCord
{
return xCord;
}

- (int) yCord
{
return yCord;
}


@end

// ---- Program Section ----

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
XYPoint *cord1 = [[XYPoint alloc] init];
XYPoint *cord2 = [[XYPoint alloc] init];
XYPoint *getSetCordMethod = [[XYPoint alloc] init];


// ---- Set CORD ----
[cord1 setXCord: 100];
[cord1 setYCord: 150];

[cord2 setXCord: 50];
[cord2 setYCord: 100];

// ---- Settes Rule the Divine ----
[getSetCordMethod setYCord: 15];
[getSetCordMethod setXCord: 10];


// ---- Display the 1st cord ----
NSLog (@"The cord is:");
[cord1 print];

// ---- Display 2nd Cord ----
NSLog(@"The cord for the 2nd is:");
[cord2 print];

//---- Display Getter Setter Method ----
NSLog(@"The getter and setter method will display this %i by %i", [getSetCordMethod xCord], [getSetCordMethod yCord]);



// ---- release ----
[cord1 release];
[cord2 release];
[getSetCordMethod release];




    [pool drain];
    return 0;
}


Think I understand it now. One question though...maybe 2.

For anything named setWhatEver
Is the name of that setter WhatEver, or is it setWhatEver
Does the word "set" have to be in it? Could just as easily be LaLaChoCho?

Also Should we get into the practice of always putting  getters and setters in our classes?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #18 on: April 16, 2009, 05:39:19 PM »

Quote
For anything named setWhatEver
Is the name of that setter WhatEver, or is it setWhatEver

The name of the setter is setWhatEver:

Quote
Does the word "set" have to be in it?

It doesn't have to be in it.  However, this is standard programming practice.  Further, when you read about synthesized accessor methods later in the book, you'll see that for an instance variable called x, the system can automatically generate a getter method called x and a setter method called setX:.

Quote
Could just as easily be LaLaChoCho?

It could be, but I wouldn't want to see the rest of your code if you picked names like that!   Shocked

Quote
Also Should we get into the practice of always putting  getters and setters in our classes?

It depends on the use of the class.  Most of the time, you will want to implement them.  However, some instance variables may just hold state information that the user of the class doesn't need to access directly, so you might not need to write a getter.  Or, the class may have an instance variable that is calculated by other methods and you don't want to set directly, so you won't need to write a setter.

Cheers,

Steve Kochan
« Last Edit: April 16, 2009, 05:41:52 PM by skochan » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #19 on: April 22, 2009, 12:04:36 PM »

If it worked, it was "random".  NSLog (like printf, scanf, or any other function that takes a variable number of arguments) has no way to know if you provided the correct number of arguments.  So it took whatever was on the stack and just displayed its value.  You got lucky!  If you were displaying an object instead of just a number and didn't supply one, you'd like get a  crash instead.

Cheers,

Steve
Logged
davej
Newbie
*
Posts: 9


« Reply #20 on: April 30, 2009, 08:45:50 AM »

Here is what I came up with for exercise 7:

/* Chapter 3 exercise 7:   Define a class called XYPoint that will hold a Cartesion
               coordinate (x, y), where x and y are integers. Define methods
               to individually set the x and y coordinates of a point and
               retrieve their values. */

#import <Foundation/Foundation.h>

// ---@interface section---

@interface XYPoint: NSObject
{
   int xCoordinate;
   int yCoordinate;
}

-(void) print;
-(void) setXCoordinate: (int) x;
-(void) setYCoordinate: (int) y;
-(int) xCoordinate;
-(int) yCoordinate;

@end

// ---@implementation section---

@implementation XYPoint

-(void) print
{
   NSLog (@"(%i,%i)", xCoordinate, yCoordinate);
}

-(void) setXCoordinate: (int) x
{
   xCoordinate = x;
}

-(void) setYCoordinate: (int) y
{
   yCoordinate = y;
}

-(int) xCoordinate
{
   return xCoordinate;
}

-(int) yCoordinate
{
   return yCoordinate;
}

@end

// ---program section---

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   XYPoint *point1 = [[XYPoint alloc] init];
   XYPoint *point2 = [[XYPoint alloc] init];

   // Set point1 to (3, 6) and point2 to (2, 5).
   
   [point1 setXCoordinate: 3];
   [point1 setYCoordinate: 6];
   
   [point2 setXCoordinate: 2];
   [point2 setYCoordinate: 5];
   
    // Display point1 using the print method.
   
    NSLog (@"point1 is:");
   [point1 print];
   
   // Display point2 using the getters xPoint and yPoint.
   
   NSLog (@"point2 is:");
   NSLog (@"(%i,%i)", [point2 xCoordinate], [point2 yCoordinate]);
   
   [point1 release];
   [point2 release];
   
    [pool drain];
    return 0;
}


Everything seemed to work out well.  It looks like the margins got a little messed up when I copied and pasted it from Xcode, but it is still legible I hope.

I did notice one thing: originally I displayed point2 using:

      NSLog (@"point 2 is: (%i,%i)", [point2 xCoordinate], [point2 yCoordinate]);

but then point1 displayed the over 2 lines and point2 on only one, so I changed it to:

      NSLog (@"point 2 is:\n(%i,%i)", [point2 xCoordinate], [point2 yCoordinate]);

but then line two of point1 was nice and even with line one because they were both proceeded by all that time and date junk, but line two of point2 was not proceeded by the time and date stamp, so it wasn't nice and pretty and all perfectly aligned, so to be a super perfectionist I changed it to the way it is in the full program above.

Is this going to be a recurring little hiccup? Is there another way around it? What determines whether or not a line is proceeded by the time and date stamp?

Love the book so far!

Learning a lot and having fun, too!


--Dave


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #21 on: April 30, 2009, 10:55:19 AM »

Dave,

Each new NSLog call causes the info to be displayed before the output.   If it really bothers you, you can switch to using printf instead (if you're familiar with that routine).

Cheers,

Steve
Logged
EnzoBro
Newbie
*
Posts: 10


« Reply #22 on: May 20, 2009, 03:05:33 PM »

I didnt do this right (confusing the setters and getters)  but I also wonder why the output is what is is.  Here is the code with the output results at the end.
Can anyone tell me why the coordinates entered were 1 and 2, but the output was 2 and 0?
EDIT: I found the answer.   Right after [XYPoint alloc] init]  I entered the value for setterX twice.  I suppose it takes the last entry (2) for X and since I never set Y it is default (0).  Thus (2,0)
Cut and Paste gets me every time.


Code: (Objective-C)
#import <Foundation/Foundation.h>

//@interface section
@interface XYPoint: NSObject
{
int xCoordinate;
int yCoordinate;
}
-(void) print;
-(void) setterX: (int)x;
-(void) setterY: (int)y;
@end

//@implementation section
@implementation XYPoint

-(void) print
{
NSLog (@"(%i,%i)", xCoordinate, yCoordinate);
}

-(void) setterX: (int)x
{
xCoordinate = x;
}

-(void) setterY: (int)y
{
yCoordinate = y;
}
@end

//program section

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// XYPoint code
XYPoint *myXYPoint = [[XYPoint alloc] init];
[myXYPoint setterX: 1];
[myXYPoint setterX: 2];
NSLog (@"My Cartesian coordinates are:");
[myXYPoint print];
[myXYPoint release];

    [pool drain];
    return 0;
}
OUTPUT IS:
 My Cartesian coordinates are:
 (2,0)

« Last Edit: May 20, 2009, 03:14:13 PM by EnzoBro » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #23 on: May 20, 2009, 03:26:51 PM »

You have a typo:  2 calls to setterX:

Code: (Objective-C)
	[myXYPoint setterX: 1];
[myXYPoint setterX: 2];

The second sets the x-coordinate to 2; the y-coordinate is unset and has its default value of 0.

Cheers,

Steve Kochan
Logged
EnzoBro
Newbie
*
Posts: 10


« Reply #24 on: May 20, 2009, 04:23:32 PM »

Yep, Thanks Steve.  I caught my error and edited my post.
So here is my final code for CH.3, Ex.7
Code: (Objective-C)
/* Chapter 3, Exercise 7
Define a class called XYPoint that will hold a Cartesian coordinate (x,y), where x and y are integers.
Define methods to infdividually set the x and y coordinates of a point and retrieve their values. 
Write and Obj-C program to implement your new class and test it.
*/

#import <Foundation/Foundation.h>

//-----@interface section-----
@interface XYPoint: NSObject
{
int xCoordinate;
int yCoordinate;
}

// unnessesary print method declaration
-(void) print;

// setters declarations
-(void) setXCoordinate: (int)x;
-(void) setYCoordinate: (int)y;

// getters declarations
-(int) xCoordinate;
-(int) yCoordinate;
@end

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

// unnessesary print method definition
-(void) print
{
NSLog (@"(%i,%i) as defined by setters(xCoordinate, yCoordinate)", xCoordinate, yCoordinate);
}

//setters definitions
-(void) setXCoordinate: (int)x
{
xCoordinate = x;
}

-(void) setYCoordinate: (int)y
{
yCoordinate = y;
}

// getters definitions
-(int) xCoordinate
{
return xCoordinate;
}

-(int) yCoordinate
{
return yCoordinate;
}
@end

//program section

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// XYPoint code
XYPoint *myXYPoint = [[XYPoint alloc] init];
[myXYPoint setXCoordinate: 1];
[myXYPoint setYCoordinate: 2];
NSLog (@"My Cartesian coordinates are:"), [myXYPoint print];
NSLog (@"My Cartesian coordinates via getters(instance variables x and y) are: (%i,%i)", [myXYPoint xCoordinate], [myXYPoint yCoordinate]);
   
[myXYPoint release];

    [pool drain];
    return 0;
}
Logged
EnzoBro
Newbie
*
Posts: 10


« Reply #25 on: May 20, 2009, 04:28:39 PM »

The point of this exercise is to show that while the setters and getters return the same value and are in essence identical...  One is the public object variable and the other is the private instance variable?

Am I close?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #26 on: May 20, 2009, 04:48:33 PM »

The point is really to just familiarize yourself with how to define a simple class and write the setters and getters.  These are the fundamental building blocks of working with classes in Objective-C.  The XYPoint class will also be used later in the text.

Cheers,

Steve Kochan
Logged
beachball
Newbie
*
Posts: 1


« Reply #27 on: June 15, 2009, 06:01:25 AM »

 ???Sorry
I am a bit lost

one thing I really don't understand is why
it is needed to identify
xcoordinate as x and  ycoordinate as y
when x & y are not used anywhere else in the script

thanks
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #28 on: June 15, 2009, 08:53:05 AM »

I don't think you posted this to the right board.  Please repost and be more specific with respect to which example/exercise you are referencing.  Then I will be able to address your question.

Cheers,

Steve Kochan
Logged
nhohmann
Global Moderator
Jr. Member
*****
Posts: 57



« Reply #29 on: June 15, 2009, 10:25:08 AM »

I don't think you posted this to the right board.  Please repost and be more specific with respect to which example/exercise you are referencing.

Actually, the first two pages of this thread cover exercise 7 (Chapter 3) the Cartesian coordinates (x & y) problem -- so it appears to be the right place for beachball to be posting.

In an attempt to answer: both x and y are defined as arguments for the following two methods within the interface and implementation sections (respectively):

// interface section

Code: (Objective-C)
-(void) setX: (int) x;
-(void) setY: (int) y;

// implementation section

Code: (Objective-C)
-(void) setX: (int) x
{
cordX = x;
}

-(void) setY: (int) y
{
cordY = y;
}

Later, in the program section, both x and y are defined.  In this case, as 34 & 88, respectively:

Code: (Objective-C)
[cord1 setX: 34];
[cord1 setY: 88];

So, in essence, both the x and y variables are utilized in all three sections of the code.  Does that make sense (and/or answer your question)?  In fact, they're used more than once since I call them using cord2 as well:

Code: (Objective-C)
[cord2 setX: -20];
[cord2 setY: 77];

Now, when the above methods are called, instance variables for both cord1 and cord2 are created that correspond to their respective x & y values.

Steve: Does that sound right?

Here's the full program:
Code: (Objective-C)
// Chapter 3 - Exercise 7 (p.48)

#import <Foundation/Foundation.h>

// ---- @interface section ----

@interface XYPoint: NSObject
{
int cordX;
int cordY;
}

-(void) print;
-(void) setX: (int) x;
-(void) setY: (int) y;
-(int) getX;
-(int) getY;

@end

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

@implementation XYPoint

-(void) print
{
NSLog(@"\n Using the 'print method' the X & Y coordinates are: %i & %i", cordX, cordY);
}

-(void) setX: (int) x
{
cordX = x;
}

-(void) setY: (int) y
{
cordY = y;
}

-(int) getX
{
return cordX;
}

-(int) getY
{
return cordY;
}

@end

// ---- Program ----


int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

XYPoint *cord1 = [[XYPoint alloc] init];
XYPoint *cord2 = [[XYPoint alloc] init];

// Set 1st coordinates to 34 & 88

[cord1 setX: 34];
[cord1 setY: 88];

// Set 2nd coordinates to -20 & 77

[cord2 setX: -20];
[cord2 setY: 77];

// Display Cord1 & Cord2 coordinates using print method

[cord1 print];
[cord2 print];

// Display Cord1 & Cord2 coordinates using the "getter" method

NSLog (@"\n Using the 'getter method' the X & Y coordinates are: %i & %i", [cord1 getX], [cord1 getY]);
NSLog (@"\n Using the 'getter method' the X & Y coordinates are: %i & %i", [cord2 getX], [cord2 getY]);

// Release memory

[cord1 release];
[cord2 release];

[pool drain];
return 0;
}
« Last Edit: June 15, 2009, 10:53:39 AM by nhohmann » Logged
Pages: 1 [2] 3 4 ... 6   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.