Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:11:00 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 ... 3 4 [5] 6   Go Down
  Print  
Author Topic: Chapter 3: Exercice program  (Read 39565 times)
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #60 on: April 22, 2010, 08:05:02 PM »

Looks good!

Cheers,

Steve
Logged
amppma
Newbie
*
Posts: 4


Email
« Reply #61 on: July 06, 2010, 05:39:00 AM »

Here's my attempt @ this exercise. Please reply.

//Program to 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;

@end

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

@implementation XYPoint

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

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

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


@end

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

int main(int argc, char *argv[])
{
   NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
   XYPoint *myXYPoint;
   
//Create an instance XYPoint
   
   myXYPoint=[XYPoint alloc];
   myXYPoint=[myXYPoint init];
   
//Set coordinate to (2,4)
   
   [myXYPoint setXCoordinate:2];
   [myXYPoint setYCoordinate:4];
   
//Display the coordinate using the print method
   
   NSLog(@"The coordinate of my XYPoint is:");
   [myXYPoint print];
   [myXYPoint release];
   
   [pool drain];
   
   return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #62 on: July 06, 2010, 05:50:58 AM »

You're not using the arguments to your setXCoordinate: and setYCoordinate: methods.  You don't want to hard-code the values into your setters.  By doing that, the actual values passed into the methods are being ignored.

Cheers,

Steve Kochan
Logged
amppma
Newbie
*
Posts: 4


Email
« Reply #63 on: July 06, 2010, 11:42:00 AM »

I'm trying to stay optimistic! How's this?

//Program to 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, char *argv[])
{
   NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
   XYPoint *myXYPoint;
   
//Create an instance XYPoint
   
   myXYPoint=[XYPoint alloc];
   myXYPoint=[myXYPoint init];
   
//Set coordinate to (2,4)
   
   [myXYPoint setXCoordinate:2];
   [myXYPoint setYCoordinate:4];
   
//Display the coordinate using the print method
   
   NSLog(@"The coordinate of XYPoint is %i, %i,", [myXYPoint xCoordinate], [myXYPoint yCoordinate]);
   [myXYPoint print];
   [myXYPoint release];
   
   [pool drain];
   
   return 0;
}
 Huh
« Last Edit: July 06, 2010, 11:44:36 AM by amppma » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #64 on: July 06, 2010, 11:43:58 AM »

Yep, that's it!

Cheers,

Steve
Logged
ibisfan33
Newbie
*
Posts: 1


« Reply #65 on: August 03, 2010, 05:43:01 PM »

Here's mine...I tried to add an additional action to it (multiplying my input) and it worked, though I have no idea if I truly wrote the code in the order that I should have:

#import <Foundation/Foundation.h>

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

@interface Product: NSObject
{
   int val1;
   int val2;
   int prod;
}
-(void) print;
-(void) setVal1: (int) v1;
-(void) setVal2: (int) v2;
-(int) val1;
-(int) val2;
@end

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

@implementation Product
-(void) print
{
   prod = val1 * val2;
   NSLog (@"%i", prod);
}

-(void) setVal1: (int) v1
{
   val1 = v1;
}

-(void) setVal2: (int) v2
{
   val2 = v2;
}

-(int) val1
{
   return val1;
}

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

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

int main (int argc, char *argv[])
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   Product *myProduct = [[Product alloc] init];
   Product *myProduct2 = [[Product alloc] init];
   
   //Set values
   [myProduct setVal1: 2];
   [myProduct setVal2: 3];
   [myProduct2 setVal1: 4];
   [myProduct2 setVal2: 5];

   //Display
   NSLog (@"%i times %i is:", [myProduct val1], [myProduct val2]);
   [myProduct print];
   NSLog (@" ");
   NSLog (@"%i times %i is:", [myProduct2 val1], [myProduct2 val2]);
   [myProduct2 print];
   [Product release];
   [pool drain];
   return 0;
}
Logged
reeceado
Newbie
*
Posts: 5


Email
« Reply #66 on: August 23, 2010, 10:32:16 AM »

Hi
could someone tell me which bits are declaring what?

Thanks



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
AdColvin
Newbie
*
Posts: 4


Email
« Reply #67 on: November 26, 2010, 02:54:55 PM »

Hi,
I built my own version of the program and it built successfully. But when I ran it in the console it didn't display the coordinates I specified. It displayed (0,0) instead of (1,3). Does anybody have an idea why? I'll show my code underneath. Thanks very much, Adam.


// A Program to Deal With X and Y Coordinates.

#import <Foundation/Foundation.h>

//-------Interface-------

@interface XYPoint : NSObject

{
   int xpoint;
   
   int ypoint;
}

- (void) setX: (int) x;
- (void) setY: (int) y;

- (int) getX;
- (int) getY;

@end

//----Implementation----

@implementation XYPoint

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


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


- (int) getX
{
   return xpoint;
}


- (int) getY
{
   return ypoint;
}

@end


//-------Program------

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   XYPoint *myPoint;
   
   myPoint = [XYPoint alloc];
   myPoint = [myPoint init];
   
   // Set coordinates to (1,3).
   
   [myPoint setX: 1];
   [myPoint setY: 3];
   
   NSLog(@"The Coordinates of myPoint are: (%i,%i).",
        [myPoint getX], [myPoint getY]);
   
   [myPoint release];
   

    [pool drain];
    return 0;
}
Logged
AdColvin
Newbie
*
Posts: 4


Email
« Reply #68 on: November 27, 2010, 03:04:57 AM »

Hi again,
I figured out what was causing the problem, but I'm not sure why this is the cause:

For instance, if I change the implementation of my setX: method as follows:

From   x = xPoint     to     xPoint = x

Then my x coordinate is displayed. The same is true for my y coordinate. So it seems that the arguments you pass to these methods will be ignored if you implement them in the form:  x = xPoint.

Why is this? Is it just the way the compiler works?
Any help much appreciated,
Thanks,
Adam
Logged
pro11
Newbie
*
Posts: 7


Email
« Reply #69 on: December 29, 2010, 11:59:03 PM »

I would really appreciate if you guys can provide feedback on this program for chapter 3 exercise 7. Thanks!!!
Let me know if theres anything I can improve on please!! Thanks again!
Code: (Objective-C)
// chapter 3-exercise7- Define a class XYPoint

#import<Foundation/Foundation.h>

//@interferance 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, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
XYPoint *myCoordinate = [[XYPoint alloc] init];

//Set values for x and y coordinates

[myCoordinate setXcoordinate: 2];
[myCoordinate setYcoordinate: 3];

//Display myCoordinates using two methods

NSLog (@"The Cartesian coordinates of X and Y is: %i and %i", [myCoordinate xcoordinate], [myCoordinate ycoordinate]);

[myCoordinate release];
[pool drain];

return 0;
}
Logged
Jake
Newbie
*
Posts: 14


« Reply #70 on: December 30, 2010, 01:27:19 AM »

Your code is solid, however Cartesian coordinates is usually expressed as a (x,y) order pair.

So change your NSLog to:

Code: (Objective-C)
NSLog (@"The Cartesian coordinates of X and Y is: (%i,%i)",  [myCoordinate xcoordinate], [myCoordinate ycoordinate]);

There are many room for improvement, but you are still at an early stage in the book so I won't talk about it now. It's great for where your knowledge stands.

If you curious of what I'm talking about, you could change the getter and setter to accessor methods and change the initialize method to initWith::. However, this is far advance for chapter 3.
Logged
aromeo
Newbie
*
Posts: 2


Email
« Reply #71 on: April 22, 2011, 08:40:41 AM »

I'm having a hard time. I looked over the example in the book, and I still have errors. The messages given are to the of code. Help





#import <Foundation/Foundation.h>

 //--- @interface section ---


@interface Coordinates: NSObject
{
    int xcoordinate;
    int ycoordinate;
}
   
-(void) print;
-(void) setxcoordinate: (int) x;
-(void) setycoordinate: (int) y;
-(int)  xcoordinate;
-(int)  ycoordinate;

@end

//---implementation section ---


@implementation Coordinates                Incomplete implementation error with a yellow exclamation
-(void) print
{
    NSLog(@"%i,%i",xcoordinate, ycoordinate);
}   

-(void) setxcooridnate:(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];
    Coordinates  *myCoordinates = [[Coordinates alloc] init];
   
    // Set coordinates to 3,5
   
    [myCoordinates setxcoordinate: 3];                  Thread 1: Program received signal "SIGABRT"
    [myCoordinates setycoordinate: 5];
   
   
    // Set the coordinate using our two new methods
   
   
    NSLog(@"The value of myCoordinate is: %i,%i", [myCoordinates xcoordinate], [myCoordinates ycoordinate]);
   
    [myCoordinates release];

    [pool drain];
    return 0;
}


Logged
mercee
Newbie
*
Posts: 6


Email
« Reply #72 on: April 29, 2011, 07:44:46 AM »

Hello i am new but maybe i could help you

i think you have a typo

Code: (Objective-C)
-(void) setxcooridnate:(int) x  
// should be setxcoordinate

Cartesian coordinates is usually expressed as a (x,y) order pair.

//like this
Code: (Objective-C)
NSLog(@"The value of myCoordinate is: (%i,%i)", [myCoordinates xcoordinate], [myCoordinates ycoordinate]);

hope this helped

Regards

Logged
sugarboyloveOC
Newbie
*
Posts: 8


« Reply #73 on: May 09, 2011, 11:53:43 PM »

Here is my first exercise, please comment.

// This is my first class exercise

#import <Foundation/Foundation.h>

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

@interface XYPoint: NSObject

{
    int coordin_x;
    int coordin_y;
}

 
-(void) coordinX: (int) x;
-(void) coordinY: (int) y;
-(int) coordin_x;
-(int) coordin_y;

@end

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

@implementation XYPoint

   
    -(void) coordinX: (int) x
{   
    coordin_x = x;
}

    -(void) coordinY: (int) y
{
    coordin_y = y;
}


-(int) coordin_x
{
    return coordin_x;
}

-(int) coordin_y
{
    return coordin_y;
}

@end

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

int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    XYPoint *cartesianXYPoint1 = [[XYPoint alloc] init];
    XYPoint *cartesianXYPoint2 = [[XYPoint alloc] init];
   
 
    // Set cartesianXYPoint1 to 25,33
   
    [cartesianXYPoint1 coordinX: 25];
    [cartesianXYPoint1 coordinY: 33];
   
    // Set cartesianXYPoint2 to 2,8
   
    [cartesianXYPoint2 coordinX: 2];
    [cartesianXYPoint2 coordinY: 8];
   
    // Display the cartesianXYPoint1&cartesianXYPoint2
   
    NSLog (@"The coordinates of cartesianXYPoint1 is %i,%i", [cartesianXYPoint1 coordin_x], [cartesianXYPoint1 coordin_y]);
    NSLog (@"The coordinates of cartesianXYPoint2 is %i,%i", [cartesianXYPoint2 coordin_x], [cartesianXYPoint2 coordin_y]);
   
    [cartesianXYPoint1 release];
    [cartesianXYPoint2 release];
   
    [pool drain];
    return 0;
}
Logged
badBandit
Newbie
*
Posts: 5


« Reply #74 on: May 11, 2011, 09:07:15 PM »

Hi!

I just couldn't wait for the 3rd edition to come out...
Is this still being moderated?

Here goes my interpretation:

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

// Define a class called XYPoint which returns cartesian coordinates of x and y.
// @interface section

@interface XYPoint: NSObject
{
    int xcoord;
    int ycoord;
}

-(void) setxcoord: (int) x;
-(void) setycoord: (int) y;
-(int) xcoord;
-(int) ycoord;

@end

// @implementation section

@implementation XYPoint

-(void) setxcoord: (int) x
{
    xcoord = x;
}
-(void) setycoord: (int) y
{
    ycoord = y;
}
-(int) xcoord
{
    return xcoord;
}
-(int) ycoord
{
    return ycoord;
}
@end


// @program section

int main (int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    XYPoint *myCoord = [[XYPoint alloc] init];
   
    // Set x, y values = 7, 9
   
    [myCoord setxcoord: 7];
    [myCoord setycoord: 9];
   
    // Display cartesian coordinates of x and y
   
    NSLog(@"The Cartesian coordinates of x and y are: \n%i and %i", [myCoord xcoord], [myCoord ycoord]);
   
    [myCoord release];
   
    [pool drain];
    return 0;
 
}

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