Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:06:01 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
  Print  
Author Topic: Chaper 7 - Operations on Fractions  (Read 2946 times)
John Shirley
Newbie
*
Posts: 47


« on: July 27, 2009, 07:43:22 AM »

Hi.

Just need a little walk through on this section on page 144.  I am a little confused with the -(void) add: (Fraction *) f;

if someone could give a little more detail as to what the
Code: (Objective-C)
(Fraction *) f;
is doing

also towards the bottom of the page the part

Code: (Objective-C)
numerator = numerator * f.denominator + denominator * f.numerator;

denominator = denominator * f.denominator;

just struggling to get my head round what its doing

Many thanks

Logged
nhohmann
Global Moderator
Jr. Member
*****
Posts: 57



« Reply #1 on: July 27, 2009, 08:35:53 AM »

(Fraction *) f is being introduced as an argument in the following "add" method:

Code: (Objective-C)
-(void) add: (Fraction *) f;

In some other, hypothetical method, the argument represented by "f" could have been introduced as an integer:

Code: (Objective-C)
-(void) otherMethod: (int) f;

But in this case, "f" needs to represent the class "Fraction" since it contains the two variables we want: (1) numerator and (2) denominator.

This is represented later, as you've pointed out, as f.numerator and f.denominator when calculating the new numerator and denominator.  (Because when the "add" method accepts (Fraction *) f it's really accepting f.numerator and f.denominator.)

If you had called the argument another name, such as "xyz", then you would have to represent the two variables as xyz.numerator and xyz.denominator.

Code: (Objective-C)
-(void) add: (Fraction *) xyz;
{
    numerator = numerator * xyz.denominator + denominator * xyz.numerator;
    denominator = denominator * xyz.denominator;
}

Does that help?  If not, hopefully someone else can explain it better.

Neil


« Last Edit: July 27, 2009, 09:10:41 AM by nhohmann » Logged
John Shirley
Newbie
*
Posts: 47


« Reply #2 on: August 11, 2009, 05:06:23 AM »

Hi Neil

Thanks for your reply and sorry its taken a while to come back to this post.  I have been away with work & then on holiday.  I have started Chapter 7 again as I did not want to rush ahead and I am back here again..

I am still not sure that I understand what is going on..

Whats the importance of having this add method of type class Fraction and not doing a add as we have done before.

Again thanks for your reply Neil.  If you or anyone could help me again here that would be awesome.

John
Logged
John Shirley
Newbie
*
Posts: 47


« Reply #3 on: August 12, 2009, 03:42:27 AM »

anyone able to help a little here.

I have moved on as I just cannot seem to get my head round it and will come back to it later  

are you creating an instance of the class Fraction and calling it f ?

Dont know if I am digging myself a little hole with this one as I have another question.

I understand how numerator and denominator get their values by using

[aFraction setTo: 1 over: 4];

&

-(void) setTo: (int) n over: (int) d
{
   numerator = n;
   denominator = d;
}

but how does f.numerator & f.denominator get their values from [bFraction setTo: 1 over: 2];

Sorry if this is really obvious.  I think I may just be looking way too hard at this section

Many thanks again
« Last Edit: August 12, 2009, 04:51:26 AM by John Shirley » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #4 on: August 12, 2009, 06:59:30 PM »

You are trying to add two fractions together.  One is the receiver of the add: message.  Its instance variables are accessible directly inside the method by name, e.g. numerator and denominator.   The second fraction, which is the argument to the add: method, is called f when the method executes.  This is based on the declaration of the add: method:

Code: (Objective-C)
-(void) add: (Fraction *) f
  ...

In order to access the instance variables of the argument f, you write f.numerator and f.denominator inside the method, since that's the name you gave to the parameter.

When you use the add: method in main with a method call like this:

Code: (Objective-C)
[aFraction add: bFraction];

The receiver of this message is aFraction and its instance variables are accessed directly when the method executed as noted by writing numerator and denominator.   The argument is the fraction object bFraction.  In the add: method it goes under the name f.

Cheers,

Steve Kochan
« Last Edit: August 12, 2009, 07:01:59 PM by skochan » Logged
John Shirley
Newbie
*
Posts: 47


« Reply #5 on: August 13, 2009, 03:09:03 AM »

Hi,  Thanks for taking the time to explain.

I think I am getting there with it

John
Logged
Pages: [1]   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.