Amazon.com Widgets I don't understand program 4.6 on page 64-65
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 16, 2014, 02:52:24 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
| |-+ Newbie Corner
| | |-+ I don't understand program 4.6 on page 64-65
Pages: [1] Go Down
Print
Author Topic: I don't understand program 4.6 on page 64-65 (Read 599 times)
mistashmoe
Newbie
*
Posts: 2






on: November 15, 2011, 09:20:13 PM

Below is the program.  I don't understand how this program works. 
I don't know where it gets the answer of 50 from the below NSLog.  I am new to the programming, i only know a little python.  I thought i was doing good until i got to this.  any help would be appreciated.
NSLog(@"The result is %g", [deskCalc accumulator]);


//
//  main.m
//  Fraction_4_6
//
//  Created by current user on 11/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//  Implement of Calculator

#import <Foundation/Foundation.h>

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

@end

@implementation Calculator

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

-(void) clear
{
    accumulator = 0;
}

-(double)   accumulator
{
    return accumulator;
}

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

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

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

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

@end


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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Calculator *deskCalc = [[Calculator alloc]  init];
   
    [deskCalc setAccumulator:100.0];
    [deskCalc add:200.];
    [deskCalc divide:15.0];
    [deskCalc subtract:10.0];
    [deskCalc multiply:5];
    NSLog(@"The result is %g", [deskCalc accumulator]);
   
    [deskCalc release];
   
   
    [pool drain];
    return 0;
}

Logged
Jemcik
Newbie
*
Posts: 22



Email




Reply #1 on: November 16, 2011, 07:56:01 AM

If don`t understand something, you should analyze it step by step.
So, let`s start (from main, of course):

1) [deskCalc setAccumulator: 100.0] - sets Accumulator to 100
take a look to the implementation:
Code: (Objective-C)
-(void) setAccumulator: (double) value
{
    accumulator = value;
}

2) [deskCalc add: 200.0]  - adds 200 to Accumulator
Code: (Objective-C)
-(void) add:  (double)  value
{
    accumulator += value;
}
Accumualor is now 300.

3)  [deskCalc divide: 15.0] - divides Accumulator on 15, so 300 / 15 = 20
Code: (Objective-C)
-(void) divide: (double) value
{
    accumulator /= value;
}

4) [deskCalc subtract: 10.0] - subtracts 10 from Accumulator, so 20 - 10 = 10
Code: (Objective-C)
-(void) subtract: (double) value
{
    accumulator -= value;
}

5) [deskCalc multiply: 5.0] - multiplies Accumulator on 5, 10 * 5 = 50
Code: (Objective-C)
-(void) multiply: (double) value
{
    accumulator *= value;
}

6) NSLog(@"The result is %g", [deskCalc accumulator]) - this simply displays Accumulator
Code: (Objective-C)
-(double)  accumulator
{
    return accumulator;
}

And of course you should know that:
accumulator *= value is equal to accumulator = value * accumulator
and about notification: %g - this means, that no signs will be shown after decimal point if there`s nothing to show Smiley
Last Edit: November 16, 2011, 08:01:51 AM by Jemcik Logged
mistashmoe
Newbie
*
Posts: 2






Reply #2 on: November 16, 2011, 10:16:01 AM

Thanks Jemcik,
I get it now.  Thank you for your help, i appreciate it. 
Logged
Jemcik
Newbie
*
Posts: 22



Email




Reply #3 on: November 16, 2011, 01:35:31 PM

Good luck in coding!
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.