Amazon.com Widgets Need help with 9.4 arithmetic
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 22, 2014, 06:43:26 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 9
| | | |-+ Need help with 9.4 arithmetic
Pages: [1] Go Down
Print
Author Topic: Need help with 9.4 arithmetic (Read 827 times)
electricb
Newbie
*
Posts: 6


Email




on: April 26, 2012, 05:50:41 PM

I've been trying to figure out this problem for over a day now, my method is called -(id) addTo (id) c. For the first step I declared result as an id object and then made that an instance of the complex class. After this step, I'm completely lost, how do we perform the arithmetic needed to define the method if we are using id objects? I can't simply use any dot notation which would make it possible to differentiate between the id objects, I can't use for example result.real = real + c.real because they are now id objects and I can't use any operators if I have to use brackets. Could somebody please toss me a bone here? This is what I have so far:

-(id) addTo:(id) c
{
    id result;
    
    result = [[Complex alloc] init];
  
    
    [result real];
    
return result;
}
Last Edit: April 26, 2012, 06:17:22 PM by electricb Logged
electricb
Newbie
*
Posts: 6


Email




Reply #1 on: April 27, 2012, 08:23:59 PM

-(id) addTo:(id)f
{
    return [self add: (id)f];
}


That's it!!?? Maybe I misunderstood the question, but I was under the impression that the add method for the Complex and Fraction classes had to be replaced with the modified add method, not used in conjunction with them. Is this solution correct, or did I miss the point entirely?
Logged
bertobettencourt
Newbie
*
Posts: 45


Email




Reply #2 on: October 23, 2013, 09:35:42 PM

So, here's how it goes:

1. You must recode your method to accept id type, you did this correctly.
Code: (Objective-C)
@property double x, y;

 -(id) addID: (id) f;

2. At implementation you must now declare what this method does, points to take into consideration:
. id type does NOT use dot operator, e.g.: sum.numerator = numerator + f.numerator. <- id DOES NOT accept this.
. id arguments use setters

Code: (Objective-C)
@synthesize x,y;

 -(id) addID: (id) f {

 id total = [Complex alloc]init]; // Here you declare you will store your values in an object called total that belongs to the class complex.

 [total setX: x + [f x]]; // remember, we cannot use the dot operator, f.x will NOT work.
 [total setY: y + [f y]]; // same as above
 
return total;


And now you just need to declare in main the rest.

Code: (Objective-C)
id firstComp, secondComp, totalComp; // declare your id variables

Complex * one = [[Complex alloc]init]; // create an object
Complex * two = [[Complex alloc]init]; // same as above

[one setTo: 2 overY: 4]; // give each object a value
[two setTo: 7 overY: 2]; // same as above

one = firstComp; // assign object one to firstComp
two = secondComp; // assign object two to secondComp

totalComp = [one addID: two]; // totalComp = one + two, we are now using the method to add two id's.

[totalComp print]; // prints whatever you declared on your method



I hope this helps you out, cheers.

Last Edit: October 23, 2013, 09:42:31 PM by bertobettencourt Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.