Amazon.com Widgets Chap. 4 Ex. 10 - got the feeling that one method is redundant
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 05:27:03 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
| | | |-+  Chap. 4 Ex. 10 - got the feeling that one method is redundant
Pages: [1]   Go Down
Print
Author Topic: Chap. 4 Ex. 10 - got the feeling that one method is redundant  (Read 993 times)
prinzenbuch
Newbie
*
Posts: 3



WWW Email




« on: September 18, 2011, 02:03:48 PM »

I think the methods memoryStore and memoryAdd are fairly similar - and only one is needed. If you look on a real calculator for instance the Mac OSX calculator – you only have four buttons: MC (to clear memory), M+ (add the displayed value to memory) M- (subtract the displayed value from memory) and MR (memory recall).

https://skitch.com/macrobat/f36bd/calculator


btw. this forum is a great idea - thank you Mr. Kochan.

greetings Matthias


Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Calculator : NSObject
{
double accumulator;
// new variable to store memory
double memory;
}

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

// arithmetic methods
-(double) add : (double) value;
-(double) subtract : (double) value;
-(double) multiply : (double) value;
-(double) divide : (double) value;
-(double) changeSign; // change sign of accumulator
-(double) reciprocal; // 1/accumulator;
-(double) xSquared; // accumulator squared

// memory methods
-(double) memory; // this method only for debug reason to see what is into memory
-(double) memoryClear;
-(double) memoryStore;
-(double) memoryRecall;
-(double) memoryAdd: (double) value;
-(double) memorySubtract: (double) value;

@end

@implementation Calculator

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

-(void) clear {
accumulator = 0;
}

-(double) accumulator {
return accumulator;
}

-(double) add:(double)value {
return accumulator += value;
}

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

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

}

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

-(double) changeSign {
return accumulator = -accumulator;
}

-(double) reciprocal {
return accumulator = 1/accumulator;
}

-(double) xSquared {
return accumulator *=accumulator;
}

// memory methods


-(double) memory { // only for debug reason
return memory;
}

// set variable memory to 0
-(double) memoryClear {
memory = 0;
return accumulator;
}

-(double) memoryStore {
memory = accumulator;
return accumulator;
}

-(double) memoryRecall {
accumulator = memory;
return accumulator;
}

-(double) memoryAdd:(double)value {
memory += value;
return accumulator;
}

-(double) memorySubtract:(double)value {
memory -= value;
return accumulator;
}


@end



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

Calculator *deskCalk;
deskCalk = [[Calculator alloc]init];

[deskCalk clear];
NSLog(@"Memory | Opperator | Display/Accumulator");
NSLog(@"M:%g |   CLR | %g" ,[deskCalk memory], [deskCalk accumulator]);
[deskCalk setAccumulator:100.0];
NSLog(@"M:%g | 100 | %g",[deskCalk memory], [deskCalk accumulator]);
NSLog(@"M:%g | + 200 | %g",[deskCalk memory], [deskCalk add:200.] );
NSLog(@"M:%g | - 10 | %g",[deskCalk memory], [deskCalk subtract:10]);
NSLog(@"M:%g | ÷ 2 | %g",[deskCalk memory], [deskCalk divide:2]);
NSLog(@"M:%g | * 3 | %g",[deskCalk memory], [deskCalk multiply:3]);
NSLog(@"M:%g | 1/x | %g",[deskCalk memory], [deskCalk reciprocal]);
NSLog(@"M:%g | 1/x | %g",[deskCalk memory], [deskCalk reciprocal]);
NSLog(@"M:%g | ± | %g",[deskCalk memory], [deskCalk changeSign]);
NSLog(@"M:%g | ± | %g",[deskCalk memory], [deskCalk changeSign]);
NSLog(@"M:%g | - 431 | %g",[deskCalk memory], [deskCalk subtract:431]);
NSLog(@"M:%g | x^2 | %g",[deskCalk memory], [deskCalk xSquared]);
NSLog(@"M:%g | x^2 | %g",[deskCalk memory], [deskCalk xSquared]);
NSLog(@"M:%g |Memory Store | %g",[deskCalk memory], [deskCalk memoryStore]);
NSLog(@"M:%g | + 10 | %g",[deskCalk memory], [deskCalk add:10.]);
NSLog(@"M:%g | M+ | %g",[deskCalk memory],[deskCalk memoryAdd:[deskCalk accumulator]]);// add the displayed value == value of accumulator to memory
NSLog(@"M:%g | - 66 | %g",[deskCalk memory],[deskCalk subtract:66]); // subtract the displayed value == value of accumulator to memory
NSLog(@"M:%g | MR | %g",[deskCalk memory],[deskCalk memoryRecall]);
NSLog(@"M:%g | M- | %g",[deskCalk memory],[deskCalk memorySubtract:[deskCalk accumulator]]);
NSLog(@"M:%g | - 22 | %g",[deskCalk memory],[deskCalk subtract:22]);

[deskCalk release];
[pool drain];
    return 0;
}
« Last Edit: September 18, 2011, 02:15:34 PM by prinzenbuch » Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: September 18, 2011, 06:23:52 PM »

Thanks for posting that.  One of the great things about writing your own apps, is that you get to do things differently when you're the one writing it.  So OUR calculator has an extra memory button to copy the accumulator into memory (on the OS X calculator you'd have to do a MC and then a M+ to do that)!

Smiley

Cheers,

Steve
P.S. I love the way you took the time to format your output!
Logged
prinzenbuch
Newbie
*
Posts: 3



WWW Email




« Reply #2 on: September 19, 2011, 01:21:31 AM »

Merci Mr. Kochan,
for your reply and your time.

This forum is a great help.

salute,
 Matthias
Logged
emygorf
Newbie
*
Posts: 4


Email




« Reply #3 on: October 15, 2011, 07:46:12 AM »

A couple of questions on this example.  The book does not seem to support some of the code, and I would like a clarification about what is good Objective-C.  (I should add that I was recommended this book, went to Amazon and bought the 2nd edition when, unknowingly, the 3rd edition was available.)

QUESTION 1:  In the above example, it seems that there is some redundant code when prinzenbuch writes

should not these be:

-(void) memoryAdd: (double) value;
-(void) memorySubtract: (double) value;

or

-(double) memoryAdd;
-(double) memorySubtract;

but not as (s)he has

-(double) memoryAdd: (double) value; 
-(double) memorySubtract: (double) value; 

I don't see this code by prizenbuch used at all in Chapter 4.

Thanks-

Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 270


WWW




« Reply #4 on: October 15, 2011, 08:10:30 AM »

Matthias' code of

-(double) memoryAdd: (double) value;  
-(double) memorySubtract: (double) value;


appears to be correct to me.  They are sending in

[deskCalk accumulator]

to

memoryAdd:

so you need the parameter of

(double) value

and they are returning the new value of

accumulator

so you need to declare the return value

(double)

and it is part of the 3rd edition exercises for Chapter 4.

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk
« Last Edit: October 15, 2011, 08:12:03 AM by fujilla » Logged
emygorf
Newbie
*
Posts: 4


Email




« Reply #5 on: October 15, 2011, 10:05:40 AM »

Thanks for adding to the discussion here.

I'll send my 2nd edition back and get the new edition coming out.

Thank you-
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.