Amazon.com Widgets Stuck on excercise 8
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2014, 10:33:27 PM
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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 4
| | | |-+ Stuck on excercise 8
Pages: [1] Go Down
Print
Author Topic: Stuck on excercise 8 (Read 1229 times)
Bo_Blomgren
Newbie
*
Posts: 7


Email




on: December 29, 2009, 03:17:53 PM

I have been struggling with exercise 8 for a while now... I have tried to modify the methods, but eventually discovered that it runs just fine to print the results without doing anything to the methods.

Here is my code. I understand this is not the right way of doing it; one should alter the methods to "double" instead of "void" and add a return statement. I must confess, however, that this is quite hard to grasp for me. I would much appreciate an explanation to what the change in methods do that isn't performed with my little trick in this code.

// Modified calculator class.
#import <Foundation/Foundation.h>

// _____The interface part_____
@interface Calculator : NSObject
{
   double accumulator;
}

// Accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

// Arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;

@end

// _____The implementation part_____

@implementation Calculator

// Implementation of accumulator methods
-(void) setAccumulator: (double) value
{
   accumulator = value;
}

-(void) clear
{
   accumulator = 0;
}

-(double) accumulator
{
   return accumulator;
}

// Implementation of arithmetic methods
-(void) add: (double) value
{
   accumulator += value;
}

-(void) subtract: (double) value
{
   accumulator -= value;
}

-(void) multiply: (double) value
{
   accumulator *= value;
}

-(void) divide: (double) value
{
   accumulator /= value;
}

@end



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   Calculator *deskCalc;
   
   deskCalc = [[Calculator alloc] init];
   
   [deskCalc clear];
   [deskCalc setAccumulator: 100.0];
    NSLog(@"The initial value is %g", [deskCalc accumulator]);
   [deskCalc add: 200];
    NSLog(@"After addition it is %g", [deskCalc accumulator]);
   [deskCalc divide:15.0];
    NSLog(@"After division it is %g", [deskCalc accumulator]);
   [deskCalc subtract:10.0];
    NSLog(@"After subtraction it is %g", [deskCalc accumulator]);
   [deskCalc multiply: 5];
   NSLog(@"And after multiplication it is %g", [deskCalc accumulator]);
   [deskCalc release];
   
    [pool drain];
    return 0;
}


Best regards,
Bo
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: December 29, 2009, 07:49:25 PM

There's nothing "wrong" with your approach; the idea that each method return the result just increases the flexibility of the methods.  This allows you to write a statement like this:

Code: (Objective-C)
NSLog(@"After division it is %g", [deskCalc divide: 15.0]);

That's all there is to it.

Cheers,

Steve Kochan
Logged
toby
Full Member
***
Posts: 104






Reply #2 on: December 30, 2009, 05:27:16 AM

It is worthwhile working to understand methods returning values, your approach works perfectly fine in this case however in some cases it will be really important for methods to return values which might for example get passed to other objects as input...
Logged
Bo_Blomgren
Newbie
*
Posts: 7


Email




Reply #3 on: January 02, 2010, 07:21:42 AM

Thank you both for your help! I think I understand this quite good now. I have been working on it a couple of more times, and now I understand the difference with methods that return values versus methods that don't.

Thanks again, and a Happy New Year to everyone!

Best regards,
Bo
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.