Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:01:30 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: Synthesize being done the wrong way?  (Read 1509 times)
amuso
Jr. Member
**
Posts: 68


Hope to make a living coding...

amuso@scw.net
« on: October 12, 2009, 01:37:16 AM »

While doing exercise 7.6 I got stuck and unstuck a few times, but finally was stuck on this problem a long time.

I decided to syntesize the getter/setters for the real and imaginary part of the number. So I removed all original code for these methods from exercise 4.6. And since I didnt want to change to value of the original complex numbers I wanted to add, I took the "hard" way and used the route to allocate and return an object from the add: method.

I also decided not to impement the setTo: method to set both the real and imaginary part in one statement.

This is what I ended up with:
Code: (Objective-C)
// Oppgave 7.6 side 156

// Lag en ny metode add: for å summere to komplekse tall basert
// på programmet i oppgave 4.6 på side 74

// Komplekse tall er tall som inneholder to komponenter. En "virkelig"
// og en "imaginær". Hvis a er virkelig, og b er imaginær kan følgende
// formel representere tallet:

// a + b i

#import <Foundation/Foundation.h>

// @interface del

@interface Complex : NSObject
{
double realPart, imaginaryPart;
}

@property double realPart, imaginaryPart;

-(void) print; // Vis som a + bi

// summer to komplekse tall:
// ta imot peker til et kompleks tall og returner peker til summen

-(Complex *) add: (Complex *) f;

@end

// @implementation del

@implementation Complex

@synthesize realPart, imaginaryPart;

-(void) print
{
NSLog(@"\n a + bi = %f + %fi", realPart, imaginaryPart);
}

-(Complex *) add: (Complex *) f
{
// Opprett nytt komplekst tall for summering
Complex *result = [[Complex alloc] init];

[result setImaginaryPart: imaginaryPart + f.imaginaryPart];
[result setRealPart: realPart + f.realPart];

return result;
}

@end

// HOVEDPROGRAM

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Opprett peker/variabel av klassen Complex

Complex *aKomplekstall = [[Complex alloc] init];
Complex *bKomplekstall = [[Complex alloc] init];
Complex *sum;

// Lag to komplekse tall

[aKomplekstall setRealPart: 15.5];
[aKomplekstall setImaginaryPart: 24.7];

[bKomplekstall setRealPart: 14.6];
[bKomplekstall setImaginaryPart: 20.2];

sum = [aKomplekstall add: bKomplekstall];
[sum print];
[sum release];

[aKomplekstall release];
[bKomplekstall release];
   
[pool drain];
    return 0;
}

Please excuse my Norwegian comments which I havent translated.

My puzzle was with this piece of code though:

Code: (Objective-C)
	[aKomplekstall setRealPart: 15.5];
[aKomplekstall setImaginaryPart: 24.7];

and similar for bKomplekstall and in the add: method when setting the real/imaginary parts there too.

For the longest time I coded:
Code: (Objective-C)
[aKomplesstall realPart: 15.5];

Then I realized I should use set, so I changed it to setrealPart, which looked wrong... but my variable starts with a lowercase letter...

And now for my question and puzzle.

Of course it should be setRealPart, but it seems to break some kind of naming pattern since we have to uppercase R in setRealPart since the variable is realPart...

Have I forgotten some magic words of wisdom from the earlier chapters, or am I doing the code all wrong perhaps since I omitted the setTo: method ?

(I hope my ramblings have made sense)
Logged

MacBook MB063 4GB OS X 10.7.3 (Xcode 4.3)
iMac MA876 3GB OS X 10.7.3
iPhone 4S 16GB 5.0.1 / iPad 2 16GB 5.0.1
Need translations to Norwegian? Happy to help.
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: October 12, 2009, 10:20:56 AM »

Look at pp. 140-141.  Also, realize you can use the dot operator with those methods.

Cheers,

Steve Kochan
Logged
amuso
Jr. Member
**
Posts: 68


Hope to make a living coding...

amuso@scw.net
« Reply #2 on: October 13, 2009, 01:32:20 AM »

Thanks! My brain wasnt working properly yesterday. I restudied this and see what you mean.

Another question about using the dot operator...

Could you use it to set multiple values instead of ie. this code?

[myRect setWidth: 5 andHeight: 8];

Something along

myRect.setWidth:setHeight...something ?
Logged

MacBook MB063 4GB OS X 10.7.3 (Xcode 4.3)
iMac MA876 3GB OS X 10.7.3
iPhone 4S 16GB 5.0.1 / iPad 2 16GB 5.0.1
Need translations to Norwegian? Happy to help.
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #3 on: October 13, 2009, 08:37:46 AM »

No the dot operator can only be used with methods that take a single argument.

Cheers,

Steve Kochan
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.