Amazon.com Widgets 4.9 Solution
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 08:19:06 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
| | | |-+  4.9 Solution
Pages: [1]   Go Down
Print
Author Topic: 4.9 Solution  (Read 1047 times)
Jason
Newbie
*
Posts: 8



Email




« on: June 01, 2010, 02:35:04 PM »

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

@interface Calculator : NSObject
{
double accumulator;
}

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

// Additional methods
-(double) changeSign; // change sign of accumulator
-(double) reciprocal; // 1/accumulator
-(double) xSquared; // accumulator sqaured
@end

@implementation Calculator

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

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return accumulator;
}

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

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

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

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

// Additional methods
-(double) changeSign
{
accumulator = 100.0;
return  -1 * accumulator;
}

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

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



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

// Set accumulator
[deskCalc clear];
NSLog(@"The accumulator has been cleared to: %g", [deskCalc accumulator]);
[deskCalc setAccumulator: 100.0];
NSLog(@"The accumulator value has been set to: %g", [deskCalc accumulator]);
// Arithmetic methods
NSLog(@"+ 50 = %g",[deskCalc add: 50.0]);
NSLog(@"- 25 = %g",[deskCalc subtract: 25.0]);
NSLog(@"x 10 = %g",[deskCalc multiply: 10.0]);
NSLog(@"/ 5 = %g",[deskCalc divide: 5.0]);
// Additional methods
[deskCalc clear];
NSLog(@"The opposite of +100 is: %g",[deskCalc changeSign]);
NSLog(@"The reciprocal of 100 is: %g",[deskCalc reciprocal]);
NSLog(@"100 squared is: %g", [deskCalc xSquared]);

[deskCalc release];

    [pool drain];
    return 0;
}
« Last Edit: June 02, 2010, 01:16:58 PM by Jason » Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #1 on: June 01, 2010, 02:55:01 PM »

Hi,

Your changeSign, xSquared, and reciprocal methods should also be setting the accumulator.

Cheers,

Steve Kochan
« Last Edit: June 02, 2010, 01:24:20 PM by skochan » Logged
Jason
Newbie
*
Posts: 8



Email




« Reply #2 on: June 02, 2010, 01:17:55 PM »

I changed the additional methods, thank you for letting me know Steve.

Cheers!
Logged
dbendixen
Newbie
*
Posts: 10


Email




« Reply #3 on: November 18, 2010, 08:39:24 PM »

I wasn't setting the accumulator either, until I read this.
Code: (Objective-C)
#import <Foundation/Foundation.h>
/* This exercise builds on example 4.6 by making the arithmetic
 methods return values instead of just doing math and saving the
 result to accumulator.  So, the returned double value could be
 used in another expression.
*/


@interface Calculator : NSObject
{
double accumulator;
}

// 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 accumulator sign
-(double) reciprocal;  // 1/accumulator
-(double) xSquared;    // accumulator squared

@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;
}

@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(@"100 + 200 =     %g", [deskCalc add: 200]);
NSLog(@"Divided by 15 = %g", [deskCalc divide: 15.0]);
NSLog(@"Subtract 15 =   %g", [deskCalc subtract: 10.0]);
NSLog(@"Multiply by 5 = %g", [deskCalc multiply: 5]);

NSLog(@"Negated = %g", [deskCalc changeSign]);
NSLog(@"Reciprocal of = %g", [deskCalc reciprocal]);
NSLog(@"Squared = %g", [deskCalc xSquared]);

[deskCalc release];

    [pool drain];
    return 0;
}
Logged
shawnpk
Newbie
*
Posts: 10






« Reply #4 on: December 10, 2010, 06:04:33 PM »

My question is not related to the code but rather to how you copy your code in color with the line numbers.  Can you please explain this as I have been trying to do this for quite awhile.  Also, can the code be printed like this also?  Thanks
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.