Amazon.com Widgets Chapter 3: Exercice program
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 01:29:30 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
| | | |-+ Chapter 3: Exercice program
Pages: 1 2 3 [4] 5 6 Go Down
Print
Author Topic: Chapter 3: Exercice program (Read 25572 times)
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #45 on: September 17, 2009, 12:47:57 AM

@Rlove1221

It is correct Smiley

When naming your setter method try to follow this convention:

1) Use the same name as the instance variable you are setting
2) Capitalize the first character
3) Add the prefix set

So the setter for xKardasian should be setXKardasian instead of setXkardasian.

Using this naming convention will be important later on when you are introduced to synthesized methods where your setters and getters are generated for you.
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
Rlove1221
Newbie
*
Posts: 5






Reply #46 on: September 19, 2009, 10:41:11 AM

   Cheesy Thanks for the info .
    I hope I'm not alone in that I read the chapter a few times to make sure I fully understand.  I'm also interested in joining the webcasts to help along with my reading.  I know the Part I is almost over but still would love to sign up so I can view the webcasts ,since the time they are currently being shown interfere with my work schedule,and if further help is needed I'd def sign up again and hopefully that class time would be better for me.
  Im determined to write an app, and as much as I would love to hurry in my learning and get one in the growing App store I know it is important to absorb programming fully first.  Im trying to keep in mind it's a marathon, not a sprint.
Logged
Rlove1221
Newbie
*
Posts: 5






Reply #47 on: September 19, 2009, 07:07:25 PM

in my answer to question chap 3#7 , whats the reason for this line to be in the implementation sect as well as the program section. Thanks in advance Smiley

-(void) print
{
   NSLog (@"(%i, %i)", xKardasian, yKardasian);
}
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #48 on: September 19, 2009, 10:07:17 PM

The implementation section defines how the method does it's work (the code).

In the program section you are telling the method to execute (run the code)
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #49 on: September 19, 2009, 10:33:26 PM

@Rlove1221

Quote
Im trying to keep in mind it's a marathon, not a sprint.

That is a very good attitude to take.

And you aren't alone with having to read chapters over a few times. I had a tough time wrapping my brain around object oriented programming (coming from a C background) at first so reading a chapter again really helped.
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
many
Newbie
*
Posts: 4







Reply #50 on: September 20, 2009, 01:58:24 PM

Hi everybody,

Here is my version. (with some Spanish in it ;-)
#import <Foundation/Foundation.h>

//-----Interface-----------//
@interface XYPoint : NSObject
{
   int abscisa;
   int ordenada;
}

- (void) print;
- (void) setAbscisa: (int) x;
- (void) setOrdenada: (int) y;
- (int) abscisa;
- (int) ordenada;

@end

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

- (void) print {
   NSLog(@"(x = %i, y = %i)", abscisa, ordenada);
}

- (void) setAbscisa: (int) x {
   abscisa = x;
}

- (void) setOrdenada: (int) y {
   ordenada = y;
}

- (int) abscisa {
   return abscisa;
}

- (int) ordenada {
   return ordenada;
}

@end

//---------Programa--------------//

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   XYPoint *XYprimera = [[XYPoint alloc] init];
   
   //dandole valor a los puntos
   [XYprimera setAbscisa: 3];
   [XYprimera setOrdenada: 7];
   
   //mostrando los valores del primer par de puntos
   NSLog(@"Los valores x y y del primer par de puntos es ");
   [XYprimera print];
   
   [XYprimera release];
   
    [pool drain];
    return 0;
}

And here you have the output:
2009-09-20 16:20:01.423 cartesian[814:10b] Los valores x y y del primer par de puntos es
2009-09-20 16:20:01.426 cartesian[814:10b] (x = 3, y = 7)

I'm using the print method to output the final values.
Question: how can I get the result in one line while using the print method of the class?

Thanks for sharing your knowledge everyone and Mr. Kochan in special
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #51 on: September 21, 2009, 11:05:33 AM

If you're a C programmer, you can printf instead of NSLog to get your output on one line.

Alternatively (and this uses stuff that's not taught yet in the text), you can write your own description method and then use the %@ format characters to print your object.  When NSLog finds the %@ format characters it will look for a method called description defined in your class and that method should return an NSString object that is the string to be displayed.  Here's what it would look like for your class:

Code: (Objective-C)
-(NSString *) description
{
      return [NSString stringWithFormat: @"(x = %i, y = %i)", abscisa, ordenada];
}

Your use would then be as follows:

Code: (Objective-C)
 NSLog(@"Los valores x y y del primer par de puntos es %@", XYprimera);

Again, this is stuff that hasn't been covered by this point.   Grin

Cheers,

Steve Kochan

Last Edit: September 21, 2009, 05:25:08 PM by skochan Logged
many
Newbie
*
Posts: 4







Reply #52 on: September 21, 2009, 05:22:52 PM

Thanks!..
Wao... So much to learn... Fantastic!
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email




Reply #53 on: October 25, 2009, 11:10:23 AM

And better late than never.. Here is my code example - very similar to others:

#import <Foundation/Foundation.h>

// Program to print map coordinates - using the "Class" function

// Interface section

@interface XYPoint: NSObject
{
   int xValue;
   int yValue;
}

-(void) print;
-(void) setxValue: (int) x;
-(void) setyValue: (int) y;
-(int) xValue;
-(int) yValue;

@end

// Implementation Section

@implementation XYPoint

-(void) print
{
   NSLog(@"%i/%i)", xValue, yValue);
}
-(void) setxValue:(int)x
{
   xValue = x;
}
-(void) setyValue:(int)y
{
   yValue = y;
}
-(int) xValue
{
   return xValue;
}
-(int) yValue
{
   return yValue;
}   

@end



// Program Section

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

   
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   XYPoint *coord = [[XYPoint alloc] init];
   
   
   // Set map coordinates
   
   [coord setxValue: 100];
   [coord setyValue: 300];
      
   
   // Display the coordinates using the print method
   
   NSLog (@"The map (hand-entered) coordinates are: %i/%i",
         [coord xValue], [coord yValue]);
   
   
   
   [coord release];
      
   [pool drain];
   return 0;
   
   
}      


It runs ok, but I have a question.... What is the purpose of the "x" and "y" variables or will I learn
that later...?

Many thanks - for the knowledge, your support, and for the fun!!!

David
Logged
prestomusic
Newbie
*
Posts: 1


Email




Reply #54 on: March 27, 2010, 01:50:26 AM

hi,

this launches in XCode, but is terminated...

// this is a program to hold x y coordinates which can be individually set

#import <Foundation/Foundation.h>

// the @interface section
@interface XYPoint: NSObject

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

@end

// the @implementation section

@implementation XYPoint
-(void) print

{
   NSLog(@"%i.%i", xCoordinate, yCoordinate);
}

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

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

}
@end
   
//program section
       
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   XYPoint *myXYPoint;
   
   //create and instance of xyPoint
   
   myXYPoint = [XYPoint alloc];
   myXYPoint = [XYPoint init];
   
   //set xyPoint to 12 and 46

   [myXYPoint setXCoordinate: 12];
   [myXYPoint setYCoordinate: 46];
   
   //display the xyPoint
   
   NSLog(@" the coordinates are:");
   [myXYPoint print];
   [myXYPoint release];
   
   [pool drain];
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #55 on: March 28, 2010, 09:01:37 AM

Hi,

alloc is a class method, that means you send it to the class (XYPoint in this case) to create a new instance for you.

Cheers,

Steve Kochan
Logged
mtube
Newbie
*
Posts: 14


Email




Reply #56 on: April 12, 2010, 10:14:16 PM

#import <Foundation/Foundation.h>
//----@interface section-----
@interface Coordinate : NSObject

{
   int Coordinate_X;
   int Coordinate_Y;
}
-(void) print;
-(void) setCoordinate_X : (int) x;
-(void) setCoordinate_Y : (int) y;
-(int)  Coordinate_X;
-(int)  Coordinate_Y;

@end

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

@implementation Coordinate
-(void) print;
{
   NSLog (@"The coordinate for the cartesian : ", Coordinate_X, Coordinate_Y);
   
}
-(void) setCoordinate_X : (int) x
{
   Coordinate_X  = x;
}
-(void) setCoordinate_Y : (int) y
{
   Coordinate_Y = y;
}
-(int) Coordinate_X
{
    return Coordinate_X;
}
-(int) Coordinate_Y
{
   return Coordinate_Y;
}
@end
   

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


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   Coordinate *myCoordinate = [[Coordinate alloc] int];
   [myCoordinate  setCoordinate_X : 30];
   [myCoordinate  setCoordinate_y : 65];
//----@Display Coordinate----
   
    NSLog(@"The value of the cartesian poiints \n..%i, \n...%i", [myCoordinate
   Coordinate_X], [myCoordinate  Coordinate_Y]);
   [myCoordinate release];
    [pool drain];
    return 0;
}
 



I entered this into my compiler and get all kinds of errors and warnings pls tell me what is wrong
I am using 3.2 xcode
Logged
mtube
Newbie
*
Posts: 14


Email




Reply #57 on: April 12, 2010, 10:32:09 PM

I corrected the myCoordinate_y with myCoordinate_Y and now it succeeded in compiling still I want feedback
#import <Foundation/Foundation.h>
//----@interface section-----
@interface Coordinate : NSObject

{
   int Coordinate_X;
   int Coordinate_Y;
}
-(void) print;
-(void) setCoordinate_X : (int) x;
-(void) setCoordinate_Y : (int) y;
-(int)  Coordinate_X;
-(int)  Coordinate_Y;

@end

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

@implementation Coordinate
-(void) print;
{
   NSLog (@"The coordinate for the cartesian : ", Coordinate_X, Coordinate_Y);
   
}
-(void) setCoordinate_X : (int) x
{
   Coordinate_X  = x;
}
-(void) setCoordinate_Y : (int) y
{
   Coordinate_Y = y;
}
-(int) Coordinate_X
{
    return Coordinate_X;
}
-(int) Coordinate_Y
{
   return Coordinate_Y;
}
@end
   

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


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   Coordinate *myCoordinate = [[Coordinate alloc] int];
   [myCoordinate  setCoordinate_X : 30];
   [myCoordinate  setCoordinate_Y : 65];
//----@Display Coordinate----
   
    NSLog(@"The value of the cartesian poiints \n..%i, \n...%i", [myCoordinate
   Coordinate_X], [myCoordinate  Coordinate_Y]);
   [myCoordinate release];
    [pool drain];
    return 0;
}
 



I entered this into my compiler and get all kinds of errors and warnings pls tell me what is wrong
I am using 3.2 xcode
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #58 on: April 13, 2010, 04:06:08 AM

You just have a simple typo in your program.  You will need to learn how to track these down.

In this line:

Code: (Objective-C)
   Coordinate *myCoordinate = [[Coordinate alloc] int];

you meant to type init instead of int.

Cheers,

Steve Kochan
Logged
hudsonr
Newbie
*
Posts: 3


Email




Reply #59 on: April 22, 2010, 01:11:08 PM

Here is mine.

#import <Foundation/Foundation.h>


// Hudson Reed
// 4/22/10
// Interface section

@interface XYPoint: NSObject
{
   int xcoordinate;
   int ycoordinate;
}


-(void) setXCoordinate: (int) x;
-(void) setYCoordinate: (int) y;
-(int) xcoordinate;
-(int) ycoordinate;

@end

// Implementation section

@implementation XYPoint

-(void) setXCoordinate: (int) x;

{
xcoordinate = x;
}

-(void) setYCoordinate: (int) y;

{
ycoordinate = y;

}
-(int) xcoordinate;

{
return xcoordinate;
}

-(int) ycoordinate;

{
return ycoordinate;
}

@end

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

// Set coordinate values   
   
   [myXYPoint setXCoordinate: 5];
   [myXYPoint setYCoordinate: 7];
   
// Display the coordinates
   
   NSLog(@"The coordinates are %i,%i",[myXYPoint xcoordinate],[myXYPoint ycoordinate]);
   
   [myXYPoint release];
   
   [pool drain];
   return 0;
}
Logged
Pages: 1 2 3 [4] 5 6 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.