Sunday 22 March 2009

Chapter 3 - Classes, Objects and Methods

Comment

So far this chapter has been conceptually difficult to understand but as the author moved on to coded examples and began to explain them it became much easier to pick up. Another neat feature of objective-c is the ability to allocate an order in which events are executed - in a similar way you would use brackets on your calculator.

The following exercises are quite theoretical and so as a result there is little example of code or syntax.


Exercises

1. Which of the following are invalid names? Why?

ANS- Int - reserved by the objective-c compiler

6_05 - cannot begin with a number (only underscore or letter)

A$ - '$' is an invalid character


_ - unsure if this is invalid but does not contain any letters or numbers so I reckon that it is


Valid names include;

_calloc,

Xx,

playNextSong,

alphaBetaRoutine,

clearScreen,

_1312,

z,

ReIntialise.


2. [...]Think of an object you use everyday. Identify a class for that object and write five actions you do with that object.


ANS - An object I use everyday is my mobile phone

A class for this could be 'Phone'

Actions:

Text

Call

Browse the web

Recharge

Take a picture


3. Given the list in exercise 2, use the following syntax to rewrite your list in this format: [instance method] ;


ANS - [myPhone text] ;

[myPhone call] ;

[myPhone browse] ;

[myPhone recharge] ;

[myPhone picture];


4. Imagine you owned a boat and a motorcycle in addition to a car. List the actions you would perform with each of these. Do you have any overlap between these actions?


ANS- actions Boat;

prepare it for use

sail it

clean it

get it serviced

dock it


actions Motorcycle

prepare to drive it

drive it

clean it

get it serviced

park it


actions Car

prepare to drive it

drive it

clean it

get it serviced

park it


There is a lot of overlap between the three sections, particularly;

prepare, clean, service and arguably sail/drive, dock/park


5. Based on question 4, imagine that you had a class called Vehicle and an object called myVehicle that could be either Car, Motorcycle, or Boat. Imagine that you wrote the following:


[myVehicle prep];

[myVehicle getGas];

[myVehicle service];


Do you see any advantage of being able to apply an action to an object that could be from one of several classes?


ANS - Yes, as no matter which vehicle is selected, they all can perform the same action which would make it simpler to compare different objects or apply the same action to all of these objects.


6. In a procedural language such as C, you think about actions and then write code to perform the action on various objects. Referring to the car example, you might write a procedure in C to wash a vehicle and then inside that procedure write code to handle washing a car, washing a boat, washing a motorcycle, and so on. If you took that approach and then wanted to add a new vehicle type (see the previous exercise), do you see advantages or disadvantages to using this procedural approach over an object-oriented approach?


ANS- An advantage would be the ability to slightly modify the procedure for washing the new vehicle type without affecting the others in place.

A disadvantage would be that the new vehicle type would not inherit the instances and methods of the previous ones and all of these would also need to be added to the new vehicle type which would be time-consuming.


7. Define a class called XYPoint that will hold a 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.


ANS - When writing this program, I learnt a valuable lesson about invalid names. At first I tried to use the '-' character in between X or Y and 'coordinate' which threw up a multitude of errors from the compiler. After resolving this issue, the program compiled and ran error free. The program has been copied and pasted from Xcode and the Console output;


#import


@interface XYPoint: NSObject

{

int Xcoordinate;

int Ycoordinate;

}


-(void) setX: (int) X;

-(void) setY: (int) Y;

-(int) Xcoordinate;

-(int) Ycoordinate;


@end


@implementation XYPoint


-(void) setX: (int) X

{

Xcoordinate = X;

}


-(void) setY: (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 *cartCoord = [[XYPoint alloc] init];

[cartCoord setX: 1];

[cartCoord setY: 2];

NSLog (@"The value of cartCoord is: (%i,%i)", [cartCoord Xcoordinate], [cartCoord Ycoordinate]);

[cartCoord release];

[pool drain];

return 0;

}


[Session started at 2009-03-23 00:16:12 +0000.]

2009-03-23 00:16:12.857 Chap3_Ex7_XYPoint[472:10b] The value of cartCoord is: (1,2)


The Debugger has exited with status 0.

No comments:

Post a Comment