Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2017, 09:49:04 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 4
| | | |-+ Stuck on excercise 8
Pages: [1] Go Down
Author Topic: Stuck on excercise 8 (Read 1615 times)
Posts: 7

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;


// _____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;


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,
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.


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

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,
Pages: [1] Go Up
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.