PDA

View Full Version : Help with adding and subtraction in Xcode




kingv84
Jun 18, 2012, 02:15 PM
Am trying to add subtraction function to my code below. Am learning how to code and I am stumped in a way to integrate addition and subtraction. Thanks for any help.


#import <Foundation/Foundation.h>


//---- @interface section----

@interface SimpleCalc: NSObject

{
int number1;

int number2;
}

-(void) setNumber1: (int) n1;
-(void) setnumber2: (int) n2;
-(int) addition;

@end


//---- @implementation section----

@implementation SimpleCalc
-(void) setNumber1:(int)n1

{
number1 = n1;
}

-(void) setnumber2:(int)n2

{
number2 = n2;
}

-(int) addition

{
return number1+number2;
}
@end


//----program section----

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

//----addition functionality----

SimpleCalc *myCalc;

myCalc = [SimpleCalc alloc];
myCalc = [myCalc init];
[myCalc setNumber1:30];
[myCalc setnumber2:30];

//message

NSLog (@"The Addition of two numbers= %i", [myCalc addition]);

[pool drain];
return 0;
}



robvas
Jun 18, 2012, 02:56 PM
I get the following output:

2012-06-18 15:56:08.590 main[9707:707] The Addition of two numbers= 60

What are you getting? What are you expecting?

kingv84
Jun 18, 2012, 03:03 PM
Thanks for the reply. Am trying to do the same thing, but with subtraction. Example- The Subtraction of two numbers = 0

I know how to make it subtract, but I want to also add subtraction to the code so the output will display the answer for addition and subtraction.



I get the following output:

2012-06-18 15:56:08.590 main[9707:707] The Addition of two numbers= 60

What are you getting? What are you expecting?

chown33
Jun 18, 2012, 03:09 PM
Add a method named subtraction. Then call it and print the result. It should work like this:
NSLog (@"The Subtraction of two numbers= %i", [myCalc subtraction]);


If you're asking "How can I write a single method that returns both the sum (addition) and difference (subtraction) of the two numbers?", the short answer is "You can't.". There are more complicated ways of returning a single object with multiple members, but they're too complex for where you're at right now, in my opinion.

Learn to make simple methods that return simple results for simple things. Learn complex methods for complex things later. Walk first, then run, then pole-vault.

kingv84
Jun 18, 2012, 03:15 PM
Thank You everyone. My problem is solved.:)

#import <Foundation/Foundation.h>


//---- @interface section----

@interface SimpleCalc: NSObject

{
int number1;

int number2;
}

-(void) setNumber1: (int) n1;
-(void) setnumber2: (int) n2;
-(int) addition;
-(int) subtraction;

@end


//---- @implementation section----

@implementation SimpleCalc
-(void) setNumber1:(int)n1

{
number1 = n1;
}

-(void) setnumber2:(int)n2

{
number2 = n2;
}

-(int) addition

{
return number1+number2;
}

-(int) subtraction

{
return number1-number2;
}
@end


//----program section----

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

//----addition functionality----

SimpleCalc *myCalc;

myCalc = [SimpleCalc alloc];
myCalc = [myCalc init];
[myCalc setNumber1:30];
[myCalc setnumber2:30];

//----subtraction----

SimpleCalc *myCalcS;

myCalcS = [SimpleCalc alloc];
myCalcS = [myCalcS init];
[myCalcS setNumber1:30];
[myCalcS setnumber2:30];


//message

NSLog (@"The Addition of two numbers= %i and the Subtraction of two numbers=%i", [myCalc addition],[myCalcS subtraction]);


[pool drain];
return 0;
}

chown33
Jun 18, 2012, 04:36 PM
Nicely done, although it's not necessary to alloc and init a second SimpleCalc object. Since both objects are given the same numbers (30 and 30), the same object can be used to give the result of addition and subtraction.

If you were giving them different numbers, even then, a single object would suffice:
1. Make the object.
2. Set two numbers for addition.
3. Show the result of addition.
4. Set two numbers for subtraction.
5. Show the result of subtraction.

At steps 3 and 5, you could also get each result and store it in a separate int variable, then show both values later.

Finally, since you're not releasing either SimpleCalc object you alloc'ed, that means you're leaking them both. It's unimportant in such a simple program, but you will eventually need to understand how memory management works. I mention this mainly for completeness and for future reference.