Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 17, 2018, 08:54:04 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
Author Topic: exercise 7, my code not good enough  (Read 2769 times)
« on: September 30, 2009, 07:41:58 PM »

Hi Steve,

I got the book on the weekend and this is my fourth day. I have spent most of it struggling with Chapter 3. My attempt at the answer to Q7 is below. It compiles without errors and gives output of two strings on two lines rather than one.

I'm generally not happy with my code as there is a lot I don't understand and that is reflected in it. I can identify most parts of the code (argument, method, argument name) etc but I'm stilly hazy on how it all integrates. It seems very complex piece of code for something so simple. I guess I'm most confused about the relationship between the implementation of the methods and declaration of the methods. Seems very convoluted to me at this stage!


#import <Foundation/Foundation.h>
@interface XYCoord: NSObject //declare new class and parent class
{ int xCoord;
int yCoord; } //declare variables and var type

-(void) print; //declare methods to be used
-(void) setxCoord: (int) x; //return value type; method: argument value type and name
-(void) setyCoord: (int) y;

//implementation- METHOD DEFINITIONS
@implementation XYCoord: NSObject
-(void) print
{ NSLog (@"%i.%i ", xCoord, yCoord);} //print the string 24.48

-(void) setxCoord: (int) x
{xCoord =x;}
-(void) setyCoord: (int) y
{yCoord =y;}

int main (int argc, const char * argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  XYCoord *myXYCoord;
  //create instance of a XYCoord
  myXYCoord = [XYCoord alloc];
  myXYCoord = [myXYCoord init];
  //set XYCoord to 24.48
  [myXYCoord setxCoord: 24];
  [myXYCoord setyCoord: 48];
  //display the coordiate using the print method
  NSLog (@"The XYCoordinate value is: ");
  [myXYCoord print];
  [myXYCoord release];
  [pool drain];
return 0;}
« Reply #1 on: September 30, 2009, 10:36:21 PM »

Here is the output:

RosiesComputer:~ rosie$ /Users/rosie/Documents/xy/build/Debug/xy ; exit;
2009-10-01 15:05:23.259 xy[3822:10b] The XYCoordinate value is:
2009-10-01 15:05:23.264 xy[3822:10b] 24.48

[Process completed]
Hero Member
Posts: 3114

« Reply #2 on: October 01, 2009, 05:20:02 AM »


It looks good, although I would work on the formatting a little to be more consistent with common formatting styles.

It does seem like a lot of work right now.  As you'll soon see, the interface and implementation sections are typically put into separate files.   The former is given to other users of the class to use (via #import), while the latter is typically stored in a library that is  linked with the program.  This separates the information about the class (its instance variables and methods) from the code that implements the methods, which is a good thing.


Steve Kochan
Full Member
Posts: 104

« Reply #3 on: October 01, 2009, 07:47:50 AM »

Seems very convoluted to me at this stage!

As Steve said the code looks pretty ok other than the formatting could be clearer. In addition to the points he made already about interface and implementation being separated it might be beneficial to note that allot of the concepts in this chapter are not originally intended to build short, quick and disposable programs. The concepts of working with objects and also separate interfaces and implementations are designed to make bigger programs more organised and structured. In doing so correctly many bigger programs can contain reusable objects that can be used in other programs saving allot of time (this is where the interface being separate starts to come in handy), it allows teams to more easily work on different parts of a program without as much concern as to whether the part that one person writes will mess up a part another person writes (again the interface file makes this easier), it means that if you update your program to fix a bug then provided you keep the interface the same you can resolve anything within the implementation part without causing havoc in other parts of your program, and also means that you don't have to repeat what is almost the same code several times in a program. While at this stage it may seem like a convoluted way of doing things if you stick with it then in a few chapters time it will start becoming very clear how much power and structure this system offers compared to a procedural approach (which typically comes more natural at the beginning).
« Reply #4 on: October 01, 2009, 06:16:33 PM »

Thankyou for the feedback Steve and Toby,

Apologies for being naughty with the formatting. I find it a lot easier to read if I put the curly braces on the same lines as the code and I hope it wont' be held against me. It also limits the scrolling I have to do by making the page shorter. I suffer a fair bit of eye strain. My main concern atm is understanding the logic of the code and terminology as I'm finding it a bit impenetrable. I've spent a lot of time just going through all the programs and commenting in descriptions of what the various bits are to try to get it through my brain as it seems to be in one ear and out the other.

I appreciate your explanation Toby.

Posts: 6

« Reply #5 on: November 03, 2009, 09:53:42 PM »

Making an attempt, running a few days behind...

#import <Foundation/Foundation.h>

   @interface XYPoint: NSObject
      int xcoordinate;
      int ycoordinate;
   -(int) xcoordinate;
   -(int) ycoordinate;
   -(void) setXCoordinate: (int) x;
   -(void) setYCoordinate: (int) y;

   @implementation XYPoint
   -(int) xcoordinate
      return xcoordinate;
   -(int) ycoordinate
      return ycoordinate;
   -(void) setXCoordinate: (int) x
      xcoordinate = x;
   -(void) setYCoordinate: (int) y
      ycoordinate = y;

   int main (int argc, const char * argv[]) {
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      XYPoint *aXYPoint = [[XYPoint alloc] init];
      [aXYPoint setXCoordinate: 2];
      [aXYPoint setYCoordinate: 3];
      NSLog(@"The Coordinates are %i,%i", [aXYPoint xcoordinate], [aXYPoint ycoordinate]);
      [aXYPoint release];
      [pool drain];
      return 0;
Pages: [1]   Go Up
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 All rights reserved.