Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 26, 2018, 04:06:05 PM *
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]   Go Down
  Print  
Author Topic: My attempt at Chap 3 Ex 7  (Read 2373 times)
WANGOOROO
Newbie
*
Posts: 11

wangooroo@hotmail.com WANGOOROO WANGOOROO
WWW Email
« on: November 04, 2009, 09:56:44 AM »


Here is my source for Chap 3 Ex 7. 
It works, however, not sure why we need a print method for the implementation section. 
When I use NSLog in the main section of the program I am getting the output using:
   
    NSLog(@"\n\nThe coordinates of XYPoint are: %i,%i", [xyCoords xVal], [xyCoords yVal]);

I tried using:
    NSLog(@"\n\nThe coordinates of XYPoint are: ", [xyCoords print]);
but that does not output anything, Xcode shows "invalid use of void expression"
So what is this print method for? 

/*
 
 Chapter 3 Exercise 7
 
 Define a class called XYPoint that will hold cartesian 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.  Write an Objective-C program to implement your
 new class and test it.
 
 */


#import <Foundation/Foundation.h>

/***** Define new class XYPoint *****/
@interface XYPoint : NSObject
{
   // Declare variables of type int for x and y
   int xVal;
   int yVal;
}

/***** Define instance methods for the XYPoint class *****/
-(void) print;
-(void) setxVal: (int) x;
-(void) setyVal: (int) y;
-(int) xVal;
-(int) yVal;
@end


/***** Define methods *****/
@implementation XYPoint
-(void) print;
{
   /* Not sure why a print method is needed here. 
    Previously used:
    NSLog(@"%i,%i", xVal, yVal);
    However, leaving this method empty yields the correct output.   
   */
}

-(void) setxVal: (int) x
{
   xVal = x;
}

-(void) setyVal: (int) y
{
   yVal = y;
}

-(int) xVal
{
   return xVal;
}

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

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

// Allocate and initialize an instance of XYPoint class
   XYPoint *xyCoords =[[XYPoint alloc] init];
   
// Set values for x and y   
   [xyCoords setxVal: 19];
   [xyCoords setyVal: 65];
   
// Display the coordinates of x and y   
   NSLog(@"\n\nThe coordinates of XYPoint are: %i,%i", [xyCoords xVal], [xyCoords yVal]);
   
   /* This line does not seem to work...
   NSLog(@"\n\nThe coordinates of XYPoint are: ", [xyCoords print]);
   */
   
   [xyCoords release];
   [pool drain];
    return 0;
}

Logged

I am striving to be the kind of person such that when my feet hit the floor each morning the devil says, "Oh crap, He's awake!"
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: November 04, 2009, 10:03:25 AM »

With the print method, you would just write this:

Code: (Objective-C)
[xyCoords print];

since the method does the NSLog call.

Cheers,

Steve Kochan
Logged
WANGOOROO
Newbie
*
Posts: 11

wangooroo@hotmail.com WANGOOROO WANGOOROO
WWW Email
« Reply #2 on: November 04, 2009, 10:17:21 AM »


Ah that makes sense now.  The method was already defined with the NSLog statement so it was redundant.

I have changed the code to now display results both ways.  With and without the print method. 

I also think my setters/getters syntax was wrong on my initial code, I have changed them now to use the standardized format setXVal etc,  But did I do it in too many places?  Is this done in both the @interface section and the @implementation section?  Kinda confused as to where setters/getters really reside.

New code is here:
/*
 
 Chapter 3 Exercise 7
 
 Define a class called XYPoint that will hold cartesian 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.  Write an Objective-C program to implement your
 new class and test it.
 
 */


#import <Foundation/Foundation.h>

/***** Define new class XYPoint *****/
@interface XYPoint : NSObject
{
   // Declare variables of type int for x and y
   int xVal;
   int yVal;
}

/***** Define instance methods for the XYPoint class *****/
-(void) print;
-(void) setXVal: (int) x;
-(void) setYVal: (int) y;
-(int) xVal;
-(int) yVal;
@end


/***** Define methods *****/
@implementation XYPoint
-(void) print;
{
    NSLog(@"\n\nThe coordinates of XYPoint are: %i,%i\n\n", xVal, yVal);
}

-(void) setXVal: (int) x
{
   xVal = x;
}

-(void) setYVal: (int) y
{
   yVal = y;
}

-(int) xVal
{
   return xVal;
}

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

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

// Allocate and initialize an instance of XYPoint class
   XYPoint *xyCoords =[[XYPoint alloc] init];
   
// Set values for x and y   
   [xyCoords setXVal: 19];
   [xyCoords setYVal: 65];
   
// Display the coordinates of x and y without using the defined print method...   
   NSLog(@"\n\nThe coordinates of XYPoint are: %i,%i\n\n", [xyCoords xVal], [xyCoords yVal]);

// Display again using the print method defined...
   [xyCoords print];
   
   [xyCoords release];
   [pool drain];
    return 0;
}
Logged

I am striving to be the kind of person such that when my feet hit the floor each morning the devil says, "Oh crap, He's awake!"
Pages: [1]   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.