Amazon.com Widgets Exercises 4.8 and 4.9
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 01, 2014, 07:26:53 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
|-+ 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 4396 times)
estschudi
Newbie
*
Posts: 15


Email




on: December 14, 2011, 06:51:01 AM

Hello,  here is what I have for exercises 8 and 9 from chapter 4.  They are combined into one and both pertain to the Calculator class example.  Thanks.

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

@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;
-(double) changeSign;
-(double) reciprical;
-(double) xSquared;

// Display accumulator

-(void) print;


@end

@implementation Calculator
{
    double accumulator;
}

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

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

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

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

-(void) print
{
    NSLog(@"Our final result is %g", accumulator);
}

@end

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

    @autoreleasepool {
       
        Calculator *deskCalc = [[Calculator alloc] init];
        [deskCalc add: 10.5];      // 0 + 10.5 = 10.5
        [deskCalc multiply: 6];    // 10.5 * 6 = 63
        [deskCalc divide: 9];      // 63 / 9 = 7
        [deskCalc multiply: 840];  // 7 * 840 = 5880
        [deskCalc subtract: 1470]; // 5880 - 1470 = 4410
        [deskCalc divide: 7.35];   // 4410 / 7.35 = 600
        [deskCalc changeSign];     // 600 becomes -600
        [deskCalc changeSign];     // -600 becomes 600
        [deskCalc subtract: 598];  // 600 - 598 = 2
        [deskCalc reciprical];     // 2 becomes 0.5
        [deskCalc xSquared];       // 0.5 ^2 = 0.25
        [deskCalc print];          // print method displays the final result of 0.25
       
       
       
   
       
    }
    return 0;
}



Quote
2011-12-14 05:49:05.404 Ch4CalculatorEx[298:707] Our final result is 0.25

Logged
LochAwe
Newbie
*
Posts: 9






Reply #1 on: December 18, 2011, 06:32:31 AM

I haven't done exercise 9 yet, but here's what I did for Exercise 8. It was easy enough to figure out how to get the methods to return the value, but I also wanted the NSLog to print what happened after performing each calculation. That wasn't so easy, as you can see by my comments in the code below, made as I experimented with various ways of doing this.

I'm sure there's a way to do it without typing in the value added or subtracted, etc., directly into the NSLog statement, but I haven't figured out how yet.

Oh, yes, I also realised I need to review the concepts of using %d and %g, etc.  I'll get it eventually. My mind works in mysterious ways.



Code: (Objective-C)
// ---- @@ interface section -----

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

@implementation Calculator
{
    double accumulator;
}

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

// ---- @@ main program -------

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

    @autoreleasepool {
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
        [deskCalc setAccumulator: 100.0];
       
        // print the value of accumulator
       
        NSLog(@"The result is %g.", [deskCalc accumulator]);
       
       [deskCalc add: 200.];
       
        // print the result
       
        NSLog(@"The result of adding %d is %g.", 200, [deskCalc accumulator]);
        /*
         
         If you want to display the amount you added (or subtracted, etc.,) you can't put:
         
          NSLog(@"The result of adding %g is %g.", [deskCalc add:200], [deskCalc accumulator]);
         
         because it'll add 200 again as NSLog evaluates the expression:
         
         2011-12-18 13:16:33.349 Ch4CalculatorClass[15157:707] The result of adding 500 is 500.
         
            (You also can't just use the NSLog alone (without calling the [deskCalc add:200.]; method) to evaluate the expression if you want it to print the number you added. If you do, you get:
         
            2011-12-18 13:17:29.306 Ch4CalculatorClass[15273:707] The result of adding 300 is 300.
         
         So if you want to display the amount you added in the NSLog expression, you need to call the method separately, then do the NSLog, and you have to put the specific value you added in the NSLog expression:
         
         2011-12-18 13:21:33.582 Ch4CalculatorClass[15795:707] The result of adding 200 is 300.
         
            However, using %g if you put a specific value in (of 200, for example) will return this:
         
            2011-12-18 13:12:30.118 Ch4CalculatorClass[14616:707] The result of adding 300 is 300.
         
            You have to use %d if you're putting a specific value in the NSLog expression, so the result is:
         
            2011-12-18 13:14:06.108 Ch4CalculatorClass[14843:707] The result of adding 200 is 300.
         
         */
       
        [deskCalc divide: 15.0];
       
        // print the result
        NSLog(@"The result of dividing by %d is %g.", 15, [deskCalc accumulator]);
       
        [deskCalc subtract: 10.0];
        // print the result
        NSLog(@"The result of subtracting %d is %g.", 10, [deskCalc accumulator]);

       
        [deskCalc multiply: 5];
        // print the result
        NSLog(@"The result of multiplying by %d is %g.", 5, [deskCalc accumulator]);

       
        NSLog(@"The result is %g.", [deskCalc accumulator]);
    }
    return 0;
}


Here's the result:

Quote
2011-12-18 13:37:07.007 Ch4CalculatorClass[17377:707] The result is 100.
2011-12-18 13:37:07.013 Ch4CalculatorClass[17377:707] The result of adding 200 is 300.
2011-12-18 13:37:07.017 Ch4CalculatorClass[17377:707] The result of dividing by 15 is 20.
2011-12-18 13:37:07.019 Ch4CalculatorClass[17377:707] The result of subtracting 10 is 10.
2011-12-18 13:37:07.020 Ch4CalculatorClass[17377:707] The result of multiplying by 5 is 50.
2011-12-18 13:37:07.022 Ch4CalculatorClass[17377:707] The result is 50.
Program ended with exit code: 0


Last Edit: December 18, 2011, 06:38:18 AM by LochAwe Logged
estschudi
Newbie
*
Posts: 15


Email




Reply #2 on: December 19, 2011, 06:46:58 AM

Looks good to me.  I know what you mean about working in mysterious ways.  I had been trying out another book and I couldn't wrap
my head around the idea of a function returning a value.   After reading the second chapter in this one I finally got it.  I've been definitely
reading the chapters more than once to get it squared away in my brain. 
Logged
mitchb
Full Member
***
Posts: 129






Reply #3 on: December 19, 2011, 10:19:02 AM

The variable declaration "double accumulator" should be in the interface section, not the implementation section.

In the implementation of the arithmetic methods can changed to one line, for example:
- (double) add: (double) value
{
        return accumulator += value;
}

 Another comment is in the NSLog statements. The values that you are using are hard coded in and not variables so to be consistent do the same thing in the NSLog statement. For example instead of this:

NSLog(@"The result of adding %d is %g.", 200, [deskCalc accumulator]);

do this

NSLog(@"The result of adding 200 is %g.", [deskCalc accumulator]);

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
ljaygould
Newbie
*
Posts: 16






Reply #4 on: January 02, 2012, 03:01:23 PM

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...
Logged
HiDefDan
Newbie
*
Posts: 1


Email




Reply #5 on: January 06, 2012, 05:58:46 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...

In order to show that the modified methods are returning  the current value of the accumulator correctly, I did this;

Code: (Objective-C)
int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
       
        [deskCalc setAccumulator: 100.0];
        NSLog(@"\naccumulator value after add --> %g",[deskCalc add: 200.]);
        NSLog(@"\naccumulator value after divide --> %g",[deskCalc divide: 15.0]);
        NSLog(@"\naccumulator value after subtract --> %g", [deskCalc subtract: 10.0]);
        NSLog(@"\naccumulator value after divide --> %g", [deskCalc multiply: 5]);
        NSLog(@"\nThe result is %g", [deskCalc accumulator]);
    }
    return 0;
}

Which returns this;
Quote
2012-01-06 12:49:46.608 ch4ex8[5181:707]
accumulator value after add --> 300
2012-01-06 12:49:46.611 ch4ex8[5181:707]
accumulator value after divide --> 20
2012-01-06 12:49:46.612 ch4ex8[5181:707]
accumulator value after subtract --> 10
2012-01-06 12:49:46.612 ch4ex8[5181:707]
accumulator value after divide --> 50
2012-01-06 12:49:46.613 ch4ex8[5181:707]
The result is 50
Logged
fosterb
Newbie
*
Posts: 1






Reply #6 on: January 13, 2012, 04:19:28 PM

You could use a local variable to represent the user input and send that value in the message, then use it in NSLog.

Code: (Objective-C)
@autoreleasepool {  
       
        Calculator *deskCalc = [[Calculator alloc] init]; 
        int userInput = 100;
       
        [deskCalc setAccumulator: userInput]; 
       
        // print the value of accumulator 
       
        NSLog(@"The result is %g.", [deskCalc accumulator]); 
       
        userInput = 200.;
        [deskCalc add: userInput]; 
       
        // print the result 
        NSLog(@"The result of adding %i is %g.", userInput, [deskCalc accumulator]); 
             
        userInput = 15.0;
        [deskCalc divide: userInput]; 
       
        // print the result 
        NSLog(@"The result of dividing by %i is %g.", userInput, [deskCalc accumulator]); 
       
        userInput = 10.0;
        [deskCalc subtract: userInput]; 
        // print the result 
        NSLog(@"The result of subtracting %i is %g.", userInput, [deskCalc accumulator]); 
       
        userInput = 5.0;
        [deskCalc multiply: userInput]; 
        // print the result 
        NSLog(@"The result of multiplying by %i is %g.", userInput, [deskCalc accumulator]); 
       
       
        NSLog(@"The result is %g.", [deskCalc accumulator]); 
    } 


2012-01-13 18:15:36.106 chapter4[26922:707] The result is 100.
2012-01-13 18:15:36.111 chapter4[26922:707] The result of adding 200 is 300.
2012-01-13 18:15:36.112 chapter4[26922:707] The result of dividing by 15 is 20.
2012-01-13 18:15:36.114 chapter4[26922:707] The result of subtracting 10 is 10.
2012-01-13 18:15:36.117 chapter4[26922:707] The result of multiplying by 5 is 50.
2012-01-13 18:15:36.118 chapter4[26922:707] The result is 50.


I'm sure there's a way to do it without typing in the value added or subtracted, etc., directly into the NSLog statement, but I haven't figured out how yet.


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

    @autoreleasepool {
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
        [deskCalc setAccumulator: 100.0];
       
        // print the value of accumulator
       
        NSLog(@"The result is %g.", [deskCalc accumulator]);
       
       [deskCalc add: 200.];
       
        // print the result
       
        NSLog(@"The result of adding %d is %g.", 200, [deskCalc accumulator]);
        [deskCalc divide: 15.0];
       
        // print the result
        NSLog(@"The result of dividing by %d is %g.", 15, [deskCalc accumulator]);
       
        [deskCalc subtract: 10.0];
        // print the result
        NSLog(@"The result of subtracting %d is %g.", 10, [deskCalc accumulator]);

       
        [deskCalc multiply: 5];
        // print the result
        NSLog(@"The result of multiplying by %d is %g.", 5, [deskCalc accumulator]);

       
        NSLog(@"The result is %g.", [deskCalc accumulator]);
    }
    return 0;
}


Logged
judfio
Newbie
*
Posts: 10






Reply #7 on: February 05, 2012, 06:27:46 PM

I really like looking at all of your exercises - there are many ways to accomplish the same task aren't there!!  This is how I did exercise 8.  I defined variable in the main section so that I could easily change my values if I wanted to.  Your comments are always appreciated. ... and yes, these data types are making my head spin too. 


//chapter 4, exercise 8
//


#import <Foundation/Foundation.h>
@interface Calculator: NSObject

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

// arithmetic methods
-(double) add:      (double) value;
-(double) subtract: (double) value;
-(double) multiple: (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;
}

// for each operation, apply the value to the accumulator and return the result

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

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

-(double) multiple:(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 a, b, c, d, e;
        a=100;
        b=200;
        c=15;
        d=10;
        e=5;
       
       
       
        [deskCalc setAccumulator:100];
       
        // For each step, show the operation and the value of the accumulator.
       
        NSLog(@"%f + %f = %f", a, b, [deskCalc add:b]);
       
        NSLog(@"%f / %f = %f", [deskCalc accumulator], c, [deskCalc divide:c]);
       
        NSLog(@"%f - %f = %f", [deskCalc accumulator], d, [deskCalc subtract:d]);
                                                             
        NSLog(@"%f * %f = %f", [deskCalc accumulator], e, [deskCalc multiple:e]);
   
                   
       
    }
    return 0;
}

Logged
johnnok
Newbie
*
Posts: 3






Reply #8 on: February 10, 2012, 05:19:29 PM

@judfio

can you explain what his line exactly does? 'Cause I can see that it works, only I can't exactly get the syntax:

NSLog(@"%f / %f = %f", [deskCalc accumulator], c, [deskCalc divide:c]);

the first %f shows the accumulator value, and then you throw in the "c": what exactly is the program doing right now?
the second %f shows the divide result, but then you add ":c" : what exactly is the program doing right now?

Thanks!
regards,
johnno
Logged
judfio
Newbie
*
Posts: 10






Reply #9 on: February 11, 2012, 09:16:28 AM

It took me a little while to remember what I did - you can really pack a lot of activity in one simple statement and I couldn't remember how it worked myself.  That means I need to be adding more comments until I get better at this. 

Remembering that this exercise asks us to have the method return the accumulator after the operation is complete, the statement works like this:

%f / %f = %f

so the first %f is the accumulator prior to the operation being executed: [deskCalc accumulator]
the second %f is c which is set to 15 prior to this operation being executed. 

So what we have is "accumulator / 15" being displayed but no operation actually being executed yet;  this is done for output purposes only - to show what operation we are executing.

The third %f is where we execute the operation and then display the result which is returned by the method DIVIDE.

     In [deskCalc divide:c], the c, which is previously set to 15, is passed to the DIVIDE method and the method returns the result.

I hope that explanation helps.
Logged
Bunchadna
Newbie
*
Posts: 27






Reply #10 on: April 14, 2012, 01:43:55 PM

Thanks to all for taking the time to post - very helpful.
Logged
clouded
Full Member
***
Posts: 123






Reply #11 on: May 09, 2012, 12:49:14 PM

Here's what I have for Exercise 8:

Code: (Objective-C)
// Chapter 4 Exercise 8. 
// The add:,subtract:,multiply:,and divide: methods from Program
// 4.6 do not return a value. Modify these methods to return the
// value of the accumulator after the computation is performed.
// Test the new methods.

// Implement a Calculator class

#import <Foundation/Foundation.h>

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

-(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, char * argv[]) {
    
    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
        [deskCalc setAccumulator: 100.0];
        NSLog (@"The result after setting is %g", [deskCalc accumulator]);
        NSLog (@"The result after adding is %g", [deskCalc add: 200.]);
        NSLog (@"The result after diving is %g", [deskCalc divide: 15.0]);
        NSLog (@"The result after subtracting is %g", [deskCalc subtract: 10.0]);
        NSLog (@"The result after multiplying is %g", [deskCalc multiply: 5]);
        NSLog (@"The end result is %g", [deskCalc accumulator]);
    }
    return 0;
}
Output:

The result after setting is 100
The result after adding is 300
The result after diving is 20
The result after subtracting is 10
The result after multiplying is 50
The end result is 50
Last Edit: July 07, 2012, 02:58:36 PM by clouded Logged
clouded
Full Member
***
Posts: 123






Reply #12 on: May 09, 2012, 01:18:37 PM

Here's my exercise 9:

Code: (Objective-C)
// Chapter 4 Exercise 9. 
// After completing exercise 8,add the following methods to the
// Calculator class and test them:
//  -(double) changeSign; // change sign of accumulator
//  -(double) reciprocal; // 1/accumulator
//  -(double) xSquared; // accumulator squared

// Implement a Calculator class

#import <Foundation/Foundation.h>

@interface Calculator: NSObject

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

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

-(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 // change sign of accumulator
{
    return accumulator *= -1;
}

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

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

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
        [deskCalc setAccumulator: 100.0];
        NSLog (@"The result after setting is %g", [deskCalc accumulator]);
        NSLog (@"The result after adding is %g", [deskCalc add: 200.]);
        NSLog (@"The result after diving is %g", [deskCalc divide: 15.0]);
        NSLog (@"The result after subtracting is %g", [deskCalc subtract: 10.0]);
        NSLog (@"The result after multiplying is %g", [deskCalc multiply: 5]);
        NSLog (@"The result after changing sign is %g", [deskCalc changeSign]);
        NSLog (@"The result after reciprocal is %g", [deskCalc reciprocal]);
        NSLog (@"The result after squaring is %g", [deskCalc xSquared]);
        NSLog (@"The end result is %g", [deskCalc accumulator]);
    }
    return 0;
}
Output:

The result after setting is 100
The result after adding is 300
The result after diving is 20
The result after subtracting is 10
The result after multiplying is 50
The result after changing sign is -50
The result after reciprocal is -0.02
The result after squaring is 0.0004
The end result is 0.0004
Last Edit: May 11, 2012, 05:32:35 PM by clouded Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




Reply #13 on: May 13, 2012, 03:15:23 AM

Hello everyone.

Please look the accumulator methods.

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

Why do we have the 2nd accumulator method "clear"? It just sort of sits there and does nothing. It was in my code as a left over from I did the original coding for program 4.6. I deleted it and my program works just the same. Am I missing something?

Just for the record, here is my code with out the "clear" method.
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Calculator : NSObject

// accumulator methods
-(void) setAccumulator: (double) value; // setter method
-(double) accumulator; // getter method

// arithmetic methods
-(double) add: (double) value;
-(double) subtract: (double) value;
-(double) multiply: (double) value;
-(double) divide: (double) value;

@end



@implementation Calculator
    {
        double accumulator; // instance variable
    }

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

-(double) accumulator
    {
        return accumulator; // getter method
    }


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

@end



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

        @autoreleasepool
            {
                Calculator *deskCalc = [Calculator new];
                
                // sets the value of accumulator to 100.0
                [deskCalc setAccumulator: 100.0];
                // prints the value of the accumulator
                NSLog (@"The value of accumulator is %g.", [deskCalc accumulator]);
                
                // adds 200 to the value of accumulator
                [deskCalc add: 200.];
                // prints the sum of accumulator
                NSLog (@"The sum of accumulator is %g.", [deskCalc accumulator]);
                
                // divides the sum of accumulator by 15
                [deskCalc divide: 15.0];
                // prints the quotient of accumulator
                NSLog (@"The quotient of accumulator is %g.", [deskCalc accumulator]);
                
                // subtracts 10 from the quotient of accumulator
                [deskCalc subtract: 10.0];
                // prints the difference of accumulator
                NSLog(@"The difference of accumulator is %g.", [deskCalc accumulator]);
                
                // multiplies the difference of accumulator by 5
                [deskCalc multiply: 5];
                // prints the product of accumulator
                NSLog (@"The product of accumulator is %g.", [deskCalc accumulator]);
            }
        
        return 0;
    }

OUTPUT
  The value of accumulator is 100.
  The sum of accumulator is 300.
  The quotient of accumulator is 20.
  The difference of accumulator is 10.
  The product of accumulator is 50.


JR
Last Edit: May 13, 2012, 03:28:01 AM by JackRabbit Logged
yiorgos
Newbie
*
Posts: 1


Email




Reply #14 on: May 31, 2012, 07:23:33 AM

i had trouble realising what the exercise asked for, as I though it should function as a "real calculator" meaning asking for input etc.

Anyway after seeing that everyone here used the NSLog method I decided to use it also:

here is what I came up with, let me know what you think:



 [deskCalc setAccumulator:100.0];
        NSLog(@"First number is 100");
       
        NSLog(@"100+200=");
        [deskCalc add:200.0];
        NSLog(@"%g",[deskCalc accumulator]);
       
        NSLog(@"%g / 15 = ",[deskCalc accumulator]);
        [deskCalc divide:15.0];
        NSLog(@"%g", [deskCalc accumulator] );
       
         NSLog(@"%g - 10 = ",[deskCalc accumulator]);
        [deskCalc subtract:10.0];
        NSLog(@"%g", [deskCalc accumulator] );
       
        NSLog(@"%g * 5 = ",[deskCalc accumulator]);
        [deskCalc multiply:5];
        NSLog(@"%g", [deskCalc accumulator] );
       
        NSLog(@"The result is  %g",[deskCalc accumulator]);
           
       
2012-05-31 16:15:54.648 Calculator[3172:403] First number is 100
2012-05-31 16:15:54.651 Calculator[3172:403] 100+200=
2012-05-31 16:15:54.652 Calculator[3172:403] 300
2012-05-31 16:15:54.652 Calculator[3172:403] 300 / 15 =
2012-05-31 16:15:54.653 Calculator[3172:403] 20
2012-05-31 16:15:54.653 Calculator[3172:403] 20 - 10 =
2012-05-31 16:15:54.654 Calculator[3172:403] 10
2012-05-31 16:15:54.654 Calculator[3172:403] 10 * 5 =
2012-05-31 16:15:54.655 Calculator[3172:403] 50
2012-05-31 16:15:54.655 Calculator[3172:403] The result is  50
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.