Amazon.com Widgets My Exercise 4.8
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 19, 2013, 03:24:21 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
| | | |-+  My Exercise 4.8
Pages: [1]   Go Down
Print
Author Topic: My Exercise 4.8  (Read 744 times)
Shawn McBee
Newbie
*
Posts: 9


Email




« on: September 14, 2011, 11:26:35 AM »

Here's my take on 4.8. Please note: I am using Xcode 4.2 with ARC enabled, so I am not using release, dealloc, etc.
Thoughts/comments appreciated

Code: (Objective-C)
//

#import <Foundation/Foundation.h>

// ----- Implement a Calculator Class -----

@interface Calculator : NSObject
{
@private
    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;

@end

@implementation Calculator

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

-(void) clear
{
    accumulator = 0;
}

-(double) accumulator
{
    return accumulator;
}

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

@end

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        Calculator *deskCalc = [[Calculator alloc] init];
        
        [deskCalc setAccumulator: 100.0];
        NSLog (@"%g", [deskCalc accumulator]);
        [deskCalc add: 200.];
        NSLog (@"Plus 200 is %g", [deskCalc accumulator]);
        [deskCalc divide: 15.0];
        NSLog (@"Divided by 15 is %g", [deskCalc accumulator]);
        [deskCalc subtract: 10.0];
        NSLog (@"Minus 10 is %g", [deskCalc accumulator]);
        [deskCalc multiply: 5];
        NSLog (@"Times 5 is %g, the final result.", [deskCalc accumulator]);
        
        
        
    }
    return 0;
}


« Last Edit: September 14, 2011, 01:12:08 PM by Shawn McBee » Logged
smartalecc5
Newbie
*
Posts: 4


Email




« Reply #1 on: September 16, 2011, 05:12:14 PM »

Is this exercise 4.8?  Where is the changesign, reciprocal, and xsquared?
Logged
Shawn McBee
Newbie
*
Posts: 9


Email




« Reply #2 on: September 18, 2011, 01:55:10 PM »

Those are in 4.9. 4.8's exercise was to return the value of the accumulator after the computation is performed.
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.