Amazon.com Widgets Solution for Exercises 2-10 of Chapter 4 with zip file for the codes
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 05:46:55 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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 4
| | | |-+  Solution for Exercises 2-10 of Chapter 4 with zip file for the codes
Pages: [1]   Go Down
Print
Author Topic: Solution for Exercises 2-10 of Chapter 4 with zip file for the codes  (Read 1224 times)
danish
Newbie
*
Posts: 16


Email




« on: June 29, 2011, 06:24:36 AM »


//  ANSWER TO ALL THE END OF CHAPTER FOUR EXERCISE QUESTIONS EXCEPT QUESTION 1.
//  OUTPUT IS GIVEN AT THE END OF EACH PROGRAM
//  Created by Danish Ahmed Ansari on 29/06/11.

/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 2
//-----------------------------------------------
//  Program to convert Fahrenheit to Celcius


#import <Foundation/Foundation.h>

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

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

    float C, F = 27;
   
    C = (F - 32) / 1.8;
   
    NSLog(@"If Fahrenheit is %.2f then its temperature in celcius is = %.2f", F, C);
   
   
    [pool drain];
    return 0;
}
 
 
// OUTPUT - If Fahrenheit is 27.00 then its temperature in celcius is = -2.78
//          Program ended with exit code: 0

*/


/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 3
//-----------------------------------------------
//  Program to get the output of the said program


#import<Foundation/Foundation.h>

int main ( int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   
    char c, d;
   
    c = 'd';
    d = c;
    NSLog(@"d = %c", d);
   
    [pool drain];
    return 0;
}
 

// OUTPUT - d = d
//          Program ended with exit code: 0

*/



/*
 
//-----------------------------------------------
//  CHAPTER 4_EXERCISE 4
//-----------------------------------------------
//  Program to evaluate the polynomial equation
 

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   
    float calc, x = 2.55;
   
    calc = (3 * x * x * x) - (5 * x * x) + 6;
   
    NSLog(@"The result to the equation is = %.3f", calc);
   
    [pool drain];
   
    return 0;
}

 
//OUTPUT -  The result to the equation is = 23.232
//          Program ended with exit code: 0

*/



/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 5
//-----------------------------------------------
//  Program to evaluate the expression and give the result


#import <Foundation/Foundation.h>

int main ( int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    float calc;
   
    calc = ( 3.31e-8 + 2.01e-7) / ( 7.16e-6 + 2.-01e-8);
   
    NSLog(@"The result of the expression is = %.2e", calc);
   
    [pool drain];
    return 0;
}

// OUTPUT - The result of the expression is = 1.17e-07
//          Program ended with exit code: 0
 
*/



/*
 
//-----------------------------------------------
//  CHAPTER 4_EXERCISE 6
//-----------------------------------------------
//  Program to evaluate Complex Numbers


#import <Foundation/Foundation.h>

@interface Complex : NSObject
{
    double real;
    double imaginary;
}

-(void) setReal:        (double) a;
-(void) setImaginary:   (double) b;
-(void) print;
-(double) real;
-(double) imaginary;

@end

@implementation Complex

-(void) setReal:(double)a
{
    real = a;
}

-(void) setImaginary:(double)b
{
    imaginary = b;
}

-(void) print
{
    NSLog(@"The Complex Equation is = %g + %gi", real, imaginary);
}

-(double) real
{
    return real;
}

-(double) imaginary
{
    return imaginary;
}

@end

int main ( int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
    Complex *myComplex = [[Complex alloc] init];
   
    [myComplex setReal:2];
    [myComplex setImaginary:5];
    [myComplex print];
    [myComplex release];
   
    [pool drain];
    return 0;
}

// OUTPUT - The Complex Equation is = 2 + 5i
//          Program ended with exit code: 0

*/



/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 7
//-----------------------------------------------
//  Program to Develop a Library of Routines


#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{
    int width;
    int height;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(void) area;
-(void) perimeter;
-(int) width;
-(int) height;

@end

@implementation Rectangle

-(void) setWidth:(int)w
{
    width = w;
}

-(void) setHeight:(int)h
{
    height = h;
}

-(int) width
{
    return width;
}

-(int) height
{
    return height;
}

-(void) area
{
    int area;
    area = width * height;
    NSLog(@"Area of the rectangle is width * height = %i", area);
}

-(void) perimeter
{
    int peri;
    peri = (2 * width) + (2 * height);
    NSLog(@"Perimeter of the rectangle is 2W + 2H = %i", peri);
}

@end

int main ( int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init];
    Rectangle *myRect = [[Rectangle alloc] init];
   
    [myRect setWidth:4];
    [myRect setHeight:3];
    [myRect area];
    [myRect perimeter];
    [myRect release];
   
    [pool drain];
    return 0;
}

// OUTPUT - Area of the rectangle is width * height = 12
//          Perimeter of the rectangle is 2W + 2H = 14
//          Program ended with exit code: 0

*/



/*
 
 //-----------------------------------------------
 //  CHAPTER 4_EXERCISE 8
 //-----------------------------------------------
 //  Modify the arithmetic methods to return value to the accumulator
 
 
 #import <Foundation/Foundation.h>
 
 @interface Calculator : NSObject
 {
 double accumulator;
 }
 
 //Set,Get and Clear Methods at Interface
 -(void) setAccumulator: (double) value;
 -(void) clear;
 -(double) Accumulator;
 
 
 //Arithmetic Methods at Interface
 -(double) add: (double) value;
 -(double) subtract: (double) value;
 -(double) multiply: (double) value;
 -(double) divide: (double) value;
 
 @end
 
 @implementation Calculator
 
 
 //Set, Get and Clear Methods at Implementation
 -(void) setAccumulator:(double)value
 {
 accumulator = value;
 }
 
 -(void) clear;
 {
 accumulator = 0;
 }
 
 -(double) Accumulator
 {
 return accumulator;
 }
 
 
 //Arithmetic Methods at Implementation
 -(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
 
 
 //Main Method
 int main (int argc, const char * argv[])
 {
 
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
 Calculator *myCalc = [[Calculator alloc] init];
 
 [myCalc setAccumulator:10];
 NSLog(@"Value of accumulator = %g\n", [myCalc Accumulator]);
 
 [myCalc add:30];
 NSLog(@"Addition of 30 = %g\n", [myCalc Accumulator]);
 
 [myCalc subtract:10];
 NSLog(@"Subtraction of 10 = %g\n", [myCalc Accumulator]);
 
 [myCalc multiply:5];
 NSLog(@"Multiplication of 5 = %g\n", [myCalc Accumulator]);
 
 [myCalc divide:10];
 NSLog(@"Division of 10 = %g\n", [myCalc Accumulator]);
 
 
 
 [myCalc release];
 
 
 [pool drain];
 return 0;
 }
 
 // OUTPUT - Value of accumulator = 10
 //          Addition of 30 = 40
 //          Subtraction of 10 = 30
 //          Multiplication of 5 = 150
 //          Division of 10 = 15
 //          Program ended with exit code: 0
 
*/



/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 9
//-----------------------------------------------
//  Add 3 new methods to the Calculator Class

#import <Foundation/Foundation.h>

@interface Calculator : NSObject
{
    double accumulator;
}

//Set,Get and Clear Methods at Interface
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) Accumulator;


//Arithmetic Methods at Interface
-(double) add: (double) value;
-(double) subtract: (double) value;
-(double) multiply: (double) value;
-(double) divide: (double) value;
-(double) changesign;
-(double) reciprocal;
-(double) xSquared;

@end

@implementation Calculator


//Set, Get and Clear Methods at Implementation
-(void) setAccumulator:(double)value
{
    accumulator = value;
}

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

-(double) Accumulator
{
    return accumulator;
}


//Arithmetic Methods at Implementation
-(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 * accumulator;
}

@end


//Main Method
int main (int argc, const char * argv[])
{
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    Calculator *myCalc = [[Calculator alloc] init];
   
    [myCalc setAccumulator:10];
    NSLog(@"Value of accumulator = %g\n", [myCalc Accumulator]);
   
    [myCalc add:30];
    NSLog(@"Addition of 30 = %g\n", [myCalc Accumulator]);
   
    [myCalc subtract:10];
    NSLog(@"Subtraction of 10 = %g\n", [myCalc Accumulator]);
   
    [myCalc multiply:5];
    NSLog(@"Multiplication of 5 = %g\n", [myCalc Accumulator]);
   
    [myCalc divide:10];
    NSLog(@"Division of 10 = %g\n", [myCalc Accumulator]);
   
    [myCalc changesign];
    NSLog(@"Sign Change = %.2g\n", [myCalc Accumulator]);
   
    [myCalc xSquared];
    NSLog(@"Squared = %g\n", [myCalc Accumulator]);
       
    [myCalc reciprocal];
    NSLog(@"Reciprocal = %.2g\n", [myCalc Accumulator]);

   
    [myCalc release];
   
   
    [pool drain];
    return 0;
}

// OUTPUT - Value of accumulator = 10
//          Addition of 30 = 40
//          Subtraction of 10 = 30
//          Multiplication of 5 = 150
//          Division of 10 = 15
//          Sign Change = -15
//          Squared = 225
//          Reciprocal = 0.0044
//          Program ended with exit code: 0

*/



/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 10
//-----------------------------------------------
//  Add Memory Capability to the Calculator Class

#import <Foundation/Foundation.h>

@interface Calculator : NSObject
{
    double accumulator;
    double memoryStore;
}

//Set,Get and Clear Methods at Interface
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) Accumulator;


//Arithmetic Methods at Interface
-(double) add: (double) value;
-(double) subtract: (double) value;
-(double) multiply: (double) value;
-(double) divide: (double) value;
-(double) changesign;
-(double) reciprocal;
-(double) xSquared;

//Memory Capability
-(double) memoryClear;
-(double) memoryStore;
-(double) memoryRecall;
-(double) memoryAdd: (double) value;
-(double) memorySubtract: (double) value;

@end


@implementation Calculator


//Set, Get and Clear Methods at Implementation
-(void) setAccumulator:(double)value
{
    accumulator = value;
}

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

-(double) Accumulator
{
    return accumulator;
}


//Arithmetic Methods at Implementation
-(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 * accumulator;
}

//Memory Capability Methods

-(double) memoryClear
{
    return memoryStore = 0;
}

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

-(double) memoryRecall
{
    return accumulator = memoryStore;
}

-(double) memoryAdd:(double)value
{
    return memoryStore += value;
}

-(double) memorySubtract:(double)value
{
    return memoryStore -= value;
}

@end


//Main Method
int main (int argc, const char * argv[])
{
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    Calculator *myCalc = [[Calculator alloc] init];
   
    [myCalc setAccumulator:10];
    NSLog(@"Value of accumulator = %g\n", [myCalc Accumulator]);
   
    [myCalc add:30];
    NSLog(@"Addition of 30 = %g\n", [myCalc Accumulator]);
   
    [myCalc memoryStore];
   
    [myCalc subtract:10];
    NSLog(@"Subtraction of 10 = %g\n", [myCalc Accumulator]);
   
    [myCalc multiply:5];
    NSLog(@"Multiplication of 5 = %g\n", [myCalc Accumulator]);
   
    [myCalc divide:10];
    NSLog(@"Division of 10 = %g\n", [myCalc Accumulator]);
   
    [myCalc changesign];
    NSLog(@"Sign Change = %.2g\n", [myCalc Accumulator]);
   
    [myCalc xSquared];
    NSLog(@"Squared = %g\n", [myCalc Accumulator]);
   
    [myCalc reciprocal];
    NSLog(@"Reciprocal = %.2g\n", [myCalc Accumulator]);
   
    [myCalc memoryRecall];
    NSLog(@"Memory At Recall = %.2g\n", [myCalc memoryRecall]);
   
    [myCalc memoryAdd:20];
    NSLog(@"Memory Add 20 = %.2g\n", [myCalc memoryRecall]);
   
    [myCalc memorySubtract:10];
    NSLog(@"Memory Subtract 10 = %.2g\n", [myCalc memoryRecall]);
   
    [myCalc release];
   
    [myCalc memoryClear];
   
    [pool drain];
    return 0;
}

// OUTPUT - Value of accumulator = 10
//          Addition of 30 = 40
//          Subtraction of 10 = 30
//          Multiplication of 5 = 150
//          Division of 10 = 15
//          Sign Change = -15
//          Squared = 225
//          Reciprocal = 0.0044
//          Memory At Recall = 40
//          Memory Add 20 = 60
//          Memory Subtract 10 = 50
//          Program ended with exit code: 0

*/
Logged
danish
Newbie
*
Posts: 16


Email




« Reply #1 on: June 29, 2011, 06:31:43 AM »

Stephen...if there is anything wrong in the solutions that i have provided...please let me know...would be appreciated.. Smiley
Logged
Overcoming
Newbie
*
Posts: 7






« Reply #2 on: June 30, 2011, 11:25:31 PM »

@danish looks like there is a typo in exercise 5 which is producing a different result then what is expected from the problem. I think the answer to the problem should be 3.26e-02.

Looks like you inserted a negative sign (-) in the last statement "2.-01e-8". When I did the same, to my program, I received the answer you arrived at.

calc = ( 3.31e-8 + 2.01e-7) / ( 7.16e-6 + 2.-01e-8);
Logged
danish
Newbie
*
Posts: 16


Email




« Reply #3 on: July 01, 2011, 12:41:53 AM »

@overcoming thanks a lot for bringing the typo to my notice...just had a look at program 5, damn i did not notice the -ve sign in the denominator part.. Smiley
Logged
danish
Newbie
*
Posts: 16


Email




« Reply #4 on: July 02, 2011, 08:34:26 PM »

Corrected Solution

/*

//-----------------------------------------------
//  CHAPTER 4_EXERCISE 5
//-----------------------------------------------
//  Program to evaluate the expression and give the result


#import <Foundation/Foundation.h>

int main ( int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    float calc;
   
    calc = ( 3.31e-8 + 2.01e-7) / ( 7.16e-6 + 2.01e-8);
   
    NSLog(@"The result of the expression is = %.2e", calc);
   
    [pool drain];
    return 0;
}

// OUTPUT - The result of the expression is = 1.17e-07
//          Program ended with exit code: 0
 
*/

NOTE - Corrected the error that was on the equation calculation
----> calc = ( 3.31e-8 + 2.01e-7) / ( 7.16e-6 + 2.-01e-8);

earlier the error was a -ve sign in the denominator equation corrected equation is
----> calc = ( 3.31e-8 + 2.01e-7) / ( 7.16e-6 + 2.01e-8);

and the correct answer for this equation is 3.26e-02
Logged
rickistern
Newbie
*
Posts: 4






« Reply #5 on: July 21, 2011, 07:54:48 AM »

in exercise 10 you put the line

 [myCalc memoryClear];

after you have released myCalc. I believe that the release statement should come after this line
Logged
rapitman
Newbie
*
Posts: 1






« Reply #6 on: July 25, 2011, 07:36:17 AM »

Thanks for postings these solutions. They're helpful.
Logged
danish
Newbie
*
Posts: 16


Email




« Reply #7 on: August 22, 2011, 01:07:39 AM »

@rickstern - yes you are right first the memory should be cleared and then only should it be released...thanks for bringing that forward..will help me remember that in my future programs.. Smiley

@rapitman - Your welcome...everyones here for sharing and helping each other...  Smiley

cheers..!  Smiley
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.