Amazon.com Widgets Exercises 4.8 and 4.9
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 02:19:09 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 4
| | | |-+  Exercises 4.8 and 4.9
Pages: 1 [2]   Go Down
Print
Author Topic: Exercises 4.8 and 4.9  (Read 2750 times)
stuartjj
Newbie
*
Posts: 11






« Reply #15 on: June 02, 2012, 11:10:51 AM »

My concern is this: Exercise 8 specifically suggests to have the arithmetic methods return the value of the accumulator...and test this functionality. Obviously it's easy to display the value of the accumulator...but how can we show that the return statements we added to the add, subtract, etc. methods are doing anything? I haven't figured out a way to do this...

You're right.  Here's what I did to show the return statements we added.  I borrowed from judflo. 

The key line here is:
Quote
NSLog(@"%f / %f = %f", [deskCalc accumulator], c, [deskCalc divide:c]);
which accesses and returns the return value saved in the divide method.


[#import <Foundation/Foundation.h>
@interface Calculator: NSObject
//acumulator 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
{double accumulator, add, subtract, multiply, divide;}

-(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];
        double z, a, s, m, d;
        z=100;
        a=200;
        d=15;
        s=10;
        m=5;
       
       
       
        [deskCalc setAccumulator:z];
        NSLog(@"%f+%f=%f",[deskCalc accumulator],a,[deskCalc add:a]);
        NSLog(@"%f-%f=%f",[deskCalc accumulator],d,[deskCalc divide:d]);     
        NSLog(@"%f-%f=%f",[deskCalc accumulator],s,[deskCalc subtract:s]);
        NSLog(@"%f-%f=%f",[deskCalc accumulator],m,[deskCalc multiply:m]);

        NSLog (@"The result is %f", [deskCalc accumulator]);
       
    }
    return 0;
}
Logged
dioworld
Newbie
*
Posts: 1


Email




« Reply #16 on: July 04, 2012, 10:05:58 PM »

This is what I came up with, just wondering if this is ok...
@interface Calculator : NSObject

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

{
    double accumulator;
}

-(void) setAccumulator: (double) value
{
    accumulator = value;
    NSLog(@"The result after setting is %g", accumulator);
}

-(void) clear;
{
    accumulator = 0;
}

-(double) accumulator;
{
    return accumulator;
}

-(double) add:(double)value;
{
    accumulator += value;
    NSLog(@"The result of adding %g is %g",value, accumulator);
    return accumulator;
  
}

-(double) subtract:(double)value;
{
    accumulator -= value;
    NSLog(@"The result of subtracting %g is %g",value, accumulator);
    return accumulator;
    
}

-(double) multiply:(double)value;
{
    accumulator *= value;
    NSLog(@"The result of multiplying by %g is %g",value, accumulator);
    return accumulator;
  
}

-(double) divide:(double)value;
{
    accumulator /= value;
    NSLog(@"The result of dividing by %g is %g",value, accumulator);
    return accumulator;
  
}


@end



int main(int argc, const char * argv[])
{

    @autoreleasepool {
 Calculator *deskCalc = [[Calculator alloc] init];
        
        [deskCalc setAccumulator:100.0];
        [deskCalc add:200];
        [deskCalc divide:15];
        [deskCalc subtract:10];
        [deskCalc multiply:5];
  }
    return 0;
}

OUTPUT:
2012-07-05 12:56:31.087 prog1[1301:403] The result after setting is 100
2012-07-05 12:56:31.091 prog1[1301:403] The result of adding 200 is 300
2012-07-05 12:56:31.092 prog1[1301:403] The result of dividing by 15 is 20
2012-07-05 12:56:31.094 prog1[1301:403] The result of subtracting 10 is 10
2012-07-05 12:56:31.097 prog1[1301:403] The result of multiplying by 5 is 50
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #17 on: July 22, 2012, 03:21:10 AM »

This is mine:

Code: (Objective-C)

#import <Foundation/Foundation.h>

@interface Calculator : NSObject

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

-(double)     add: (double) value;
-(double)     subtract: (double) value;
-(double)     multiply: (double) value;
-(double)     divide: (double) value;

@end

@implementation Calculator
{
    double accumulator;
}

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

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
       
        [deskCalc setAccumulator:100.0];
        NSLog(@"\n%f + %i = %g",[deskCalc accumulator], 200, [deskCalc add:200]);
        NSLog(@"\n%f + %f = %g",[deskCalc accumulator], 15.0, [deskCalc divide:15.0]);
        NSLog(@"\n%f + %f = %g",[deskCalc accumulator], 10.0, [deskCalc subtract:10.0]);
        NSLog(@"\n%f + %i = %g",[deskCalc accumulator], 5, [deskCalc multiply:5]);
    }
    return 0;
}


Output:

2012-07-22 10:53:24.762 prog4[1977:403]
100.000000 + 200 = 300
2012-07-22 10:53:24.765 prog4[1977:403]
300.000000 + 15.000000 = 20
2012-07-22 10:53:24.765 prog4[1977:403]
20.000000 + 10.000000 = 10
2012-07-22 10:53:24.766 prog4[1977:403]
10.000000 + 5 = 50
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #18 on: July 22, 2012, 03:40:34 AM »

Exercise 4.9
Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
       
        [deskCalc setAccumulator:100.0];
        NSLog(@"\n%f + %i = %g",[deskCalc accumulator], 200, [deskCalc add:200]);
        NSLog(@"\n%f + %f = %g",[deskCalc accumulator], 15.0, [deskCalc divide:15.0]);
        NSLog(@"\n%f + %f = %g",[deskCalc accumulator], 10.0, [deskCalc subtract:10.0]);
        NSLog(@"\n%f + %i = %g",[deskCalc accumulator], 5, [deskCalc multiply:5]);
        NSLog(@"%g", [deskCalc changeSign]);
        NSLog(@"%g", [deskCalc changeSign]);
        NSLog(@"%g", [deskCalc reciprocal]);
        NSLog(@"%g", [deskCalc reciprocal]);
        NSLog(@"%g", [deskCalc xSquared]);
    }
    return 0;
}

Output:

2012-07-22 11:38:05.945 prog4[2089:403]
100.000000 + 200 = 300
2012-07-22 11:38:05.947 prog4[2089:403]
300.000000 + 15.000000 = 20
2012-07-22 11:38:05.948 prog4[2089:403]
20.000000 + 10.000000 = 10
2012-07-22 11:38:05.948 prog4[2089:403]
10.000000 + 5 = 50
2012-07-22 11:38:05.949 prog4[2089:403] -50
2012-07-22 11:38:05.949 prog4[2089:403] 50
2012-07-22 11:38:05.950 prog4[2089:403] 0.02
2012-07-22 11:38:05.951 prog4[2089:403] 50
2012-07-22 11:38:05.951 prog4[2089:403] 2500
Logged
Pages: 1 [2]   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.