Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:10:25 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 ... 6   Go Down
  Print  
Author Topic: Chapter 3: Exercice program  (Read 39564 times)
jerome
Newbie
*
Posts: 48


« on: February 14, 2009, 06:36:12 AM »

Cu Jerome
« Last Edit: February 15, 2009, 09:38:46 AM by jerome » Logged
jessmckenzie
Newbie
*
Posts: 10



WWW Email
« Reply #1 on: March 27, 2009, 05:02:31 AM »

Hey guys,
Here's my interpretation of the exercise.
NOTE: it can be done many ways... this is just my way.

Jess Grin
Logged
arun
Newbie
*
Posts: 3


« Reply #2 on: April 06, 2009, 11:20:46 AM »

Here is my attempt at the exercise. I'm very new to programming so feedback is welcome. 
/* This program is designed to create a class called XYpoint.  To define methods which can individully
   set the x and y coordinates of a point and retrieve them. */

#import <Foundation/Foundation.h>

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

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

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

@end

//---- implemantation section ----

@implementation XYPoint

-(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 *myXYPoint = [[XYPoint alloc] init];
   
    // set x and y 1,3
   
   [myXYPoint setXCoordinate: 1];
   [myXYPoint setYCoordinate: 3];
   
   // Display the coordinates -----
   
   NSLog(@"The Coordiates are %i,%i", [myXYPoint xcoordinate], [myXYPoint ycoordinate]);
   
   [myXYPoint release];
   [pool drain];
   
   return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #3 on: April 06, 2009, 01:28:37 PM »

@arun: Looks perfect to me!    Smiley

Cheers,

Steve Kochan
Logged
jkiley
Newbie
*
Posts: 43



Email
« Reply #4 on: April 09, 2009, 09:19:54 PM »

Here is my shot at it.  It works, but I'm always happy to hear suggestions.  Also, let me know if I have missed any style conventions (even the picky ones), so I can build good habits.  Steve, I know it's only three chapters for me so far, but I really like the book up to this point (from a new to programming perspective).

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

// ----- @interface Section -----

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

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

@end

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

@implementation XYPoint

- (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, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];

// Set the values of the points

[myPoint setxValue: 3];
[myPoint setyValue: 6];

// Display the coordinate

    NSLog(@"The Cartesian coordinate for myPoint is (%i,%i).", [myPoint xValue], [myPoint yValue]);
   
[myPoint release];
[pool drain];
    return 0;
}
« Last Edit: April 09, 2009, 10:23:34 PM by jkiley » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #5 on: April 09, 2009, 09:29:45 PM »

Looks great!  Convention is to capitalize the first letter of the instance variable for the name of the setter routine. So, in you case, since you have instance variable called xValue and yValue, convention would be to call the getters xValue and yValue, and the setters setXValue: and setYValue:.

Cheers,

Steve Kochan
Logged
sgg_admin
Newbie
*
Posts: 5



WWW Email
« Reply #6 on: April 11, 2009, 07:12:35 PM »

Quick question. Under the implementation section im noticing users don't have the

-(print)
{
    NSLog(@"The coordinates Blah blah");
{


Is this because it isn't needed? Because the author says their answers are correct. Just a noob who's confused here....
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #7 on: April 11, 2009, 07:19:14 PM »

Are you referring to this particular example of implementing the XYPoint class?  It's up to the implementor of the class to decide if a print method is called for.  There's no harm in adding one, but it's not called for by the exercise.

Cheers,

Steve Kochan
Logged
Intyre3
Guest
« Reply #8 on: April 13, 2009, 08:38:36 PM »

Hello everyone, here is my program for Chapter 3

#import <Foundation/Foundation.h>

/* Program to define a class called XYPoint
 to hold the Cartesian coordinate of X and Y */

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

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

-(void) setxcoordinate: (int) x;
-(void) setycoordinate: (int) y;

@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

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   XYPoint *myXYPoint = [[XYPoint alloc] init];
   
// Set coordinate to X = 1 and Y = 3
   
   [myXYPoint setxcoordinate: 1];
   [myXYPoint setycoordinate: 3];
   
// Display the Cartesian coordinates of X and Y
   
    NSLog(@"The Cartesian coordiante of X and Y is: %i and %i", [myXYPoint xcoordinate], [myXYPoint ycoordinate]);
      
    [pool drain];
    return 0;
}
Logged
Intyre3
Guest
« Reply #9 on: April 13, 2009, 08:49:28 PM »

Here is my updated program. I left out the two lines in the @interface section
-(int) xcoordinate;
-(int) ycoordinate;

It ran without those two lines but after looking at the other samples I noticed they were missing in mine.  If anyone can tell
me why they are needed and why it still ran without them I would really appriciate it.

Thanks

Ron

#import <Foundation/Foundation.h>

/* Program to define a class called XYPoint
 to hold the Cartesian coordinate of X and Y */

// ---- @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

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   XYPoint *myXYPoint = [[XYPoint alloc] init];
   
// Set coordinate to X = 1 and Y = 3
   
   [myXYPoint setxcoordinate: 1];
   [myXYPoint setycoordinate: 3];
   
// Display the Cartesian coordinates of X and Y
   
    NSLog(@"The Cartesian coordiante of X and Y is: %i and %i", [myXYPoint xcoordinate], [myXYPoint ycoordinate]);
      
    [pool drain];
    return 0;
Logged
Intyre3
Guest
« Reply #10 on: April 13, 2009, 09:04:51 PM »

Here are my answers if any are wrong please let me know.

Thanks

Ron

Chapter 3 Exercises

1.
a. Int - It is a reserved name for the Objective-C compiler.
b. 6_05 - Cant start with a number.
c. A$ - $ is not a valid character.

2.
My iPhone
make a phone call
listen to music
play a game
surf the internet
send a text message

3.
myPhone = [Phone, new];       get a new phone
[myPhone call];         make a phone call
[myPhone music];         listen to music
[myPhone game];         play a game
[myPhone internet];         surf the internet
[myPhone text};         send a text message

4. Yes

Car
Drive
Wash
Fill with Gas
Service
Spin out tires

Motorcycle
Drive
Wash
Fill with Gas
Service
Pop a wheelie

Boat
Drive
Wash
Fill with Gas
Service
Jump ramp

5. Yes It will allow you to use more than one class with a small amount of code.
6. The advantage is to an OOP because you can apply different classes to one piece of code.
7. See my Post
Logged
sgg_admin
Newbie
*
Posts: 5



WWW Email
« Reply #11 on: April 14, 2009, 06:56:42 PM »

Here's what i got. I tested it and it works but looks a little different than whats on here so im open to better form or suggestions.

Code: (Objective-C)
//Program to show coordinates

#import <Foundation/Foundation.h>

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

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

-(void) print;
-(void) setXCoordinate: (int) x;
-(void) setYCoordinate: (int) y;
@end

//---------@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 *myCoordinates = [[XYPoint alloc] init];

//set my coordinates
[myCoordinates setXCoordinate: 7];
[myCoordinates setYCoordinate: 9];

//Display my coordinates

NSLog(@"I am located at the following coordinates:");
[myCoordinates print];
[myCoordinates release];

    [pool drain];
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #12 on: April 14, 2009, 07:06:12 PM »

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.

Cheers,

Steve
Logged
Freerider
Newbie
*
Posts: 16


« Reply #13 on: April 15, 2009, 04:15:06 PM »

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

I did the same thing with my example - I was trying to break the program to try and understand it better, but have just caused more confusion:
I don't quite understand why removing the getter methods still works. You say "probably" - does this mean they're not *really* necessary?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #14 on: April 15, 2009, 04:46:01 PM »

It still works because they're not used by the test program in main.   If you wanted to access the individual x or y coordinates of your point object, you'd have to implement the getters.

Cheers,

Steve
Logged
Pages: [1] 2 3 ... 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.