Monday 30 March 2009

Chapter 4 - Data Types and Expressions

Comment

Another valuable operation of Objective-C I have found so far is the 'Type Cast Operator'. This operator converts the value of a variable to a different variable type for evaluation. This method can also be used to turn a generic object into an object of a particular class which seems like it could be a very valuable operation.


However, I am unsure as to the exact purpose of the 'Bitwise Operators'. The book did not make it clear as to their purpose in aiding programming.


I think I made a mess of most of this, plus there is quite a lot to read. So I apologize for not having this posted nice and early for you to read through.


Questions 8, 9 and 10 involve modifying the same Program which I will bring on a memory stick for you to see as I have had some troubles with it. As a result, the included evidence for these questions on here may not make much sense


Exercises


1. Which of the following are invalid constants. Why?

Valid:

123.456 - a float


2. Write a program that converts 27 degrees from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula:

C = (F - 32) / 1.8


ANS - Copy and Paste of program and console below


#import


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

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


int F = 27;

float C;

NSLog(@"The value of 27F in Degrees C is: %f", C = (F - 32) / 1.8);

[pool drain];

return 0;

}


[Session started at 2009-03-30 00:38:30 +0100.]

2009-03-30 00:38:30.549 Chap4_Ex2_tempConverter[632:10b] The value of 27F in Degrees C is: -2.777778


The Debugger has exited with status 0.


3. What output would you expect from the following program?

#import


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;

}


ANS - Expected Output: d = d


4. Write a program to evaluate the polynomial shown here:

3x^3 - 5x^2 + 6

for x = 2.55


ANS - Copy and Paste of Program and Console below.


#import


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

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


float x1 = 2.55, x2 = 2.55; //This isn't right since it equates to 2.55x10^3 instead of 2.55^3

float ans;

x1 *= x1 * x1;

x2 *= x2;

ans = 3 * x1 - 5 * x2 + 6;

NSLog(@"For x equal to 2.55 the answer is: %f", ans);

[pool drain];

return 0;

}


[Session started at 2009-03-30 02:46:36 +0100.]

2009-03-30 02:46:36.787 Chap4_Ex4_polynomial[1264:10b] For x equal to 2.55 the answer is: 23.231621


The Debugger has exited with status 0.


5. Write a program that evaluates the following expression and displays the results (remember to use the exponential format to display the result):

(3.31 x 10^-8 x 2.01 x 10^-7) / (7.16 x 10^-6 + 2.01 x 10^-8)


ANS - I couldn't get this to work correctly, I am unsure as to why however. Copy and Paste of program and console below.


#import


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

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


long double expression;

expression = 1 + (3.31e-8 * 2.01e-7) / (7.16e-6 + 2.01e-8);

NSLog(@"Result of expression is: %f", expression);

[pool drain];

return 0;

}


The Debugger has exited with status 0.

[Session started at 2009-03-30 01:20:21 +0100.]

2009-03-30 01:20:21.294 Chap4_Ex5_Expo[899:10b] Result of expression is: -0.000000


The Debugger has exited with status 0.


6. Complex numbers are numbers that contain two components: a real part and an imaginary part. If a is the real component and b is the imaginary component, this is the notation used to represent the number:

a + b i

Write an Objective-C program that defines a new class called Complex. Following the paradigm established for the Fraction class, define the following method for your new class:


-(void) setReal: (double) a;

-(void) setImaginary: (double) b;

-(void) print; // display as a + bi

-(double) real;

-(double) imaginary;


Write a test program to test your new class and methods.


ANS - Copy and Paste of Program and Console below


#import


@interface Complex: NSObject

{

int real;

int imaginary;

}


-(void) setReal: (double) a;

-(void) setImaginary: (double) b;

-(void) print; // display as a + bi

-(double) real;

-(double) imaginary;


@end


@implementation Complex


-(void) setReal: (double) a;

{

real = a;

}


-(void) setImaginary: (double) b;

{

imaginary = b;

}


-(void) print;

{

NSLog(@"Complex number equals: %d + %di", real, imaginary);

}


-(double) real;

{

return real;

}


-(double) imaginary;

{

return imaginary;

}


@end


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

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


Complex *comp1 = [[Complex alloc] init];

[comp1 setReal: 3];

[comp1 setImaginary: 2];

[comp1 print];

[comp1 release];

[pool drain];

return 0;

}


[Session started at 2009-03-30 01:46:05 +0100.]

2009-03-30 01:46:05.091 Chap4_Ex6_Complex[1021:10b] Complex number equals: 3 + 2i


The Debugger has exited with status 0.


7. Suppose you are developing a library of routines to manipulate graphical objects. Start by defining a new class called Rectangle. For now, just keep track of the rectangle's width and height. Develop methods to set the rectangle's width and height, retrieve these values, and calculate the rectangle's area and perimeter. Assume that these rectangle objects describe rectangles on an integral grid, such as a computer screen. In that case, assume that the width and height of the rectangle are integer values.


Here is the @interface section for the rectangle class:

@interface Rectangle: NSObject

{

int width;

int height;

}


-(void) setWidth: (int) w;

-(void) setHeight: (int) h;

-(int) width;

-(int) height;

-(int) area;

-(int) perimeter;

@end


Write the @implementation section and a test program to test your new class and methods.


ANS - Copy and Paste of the Program and Console below.


#import


@interface Rectangle: NSObject


{

int width;

int height;

}


-(void) setWidth: (int) w;

-(void) setHeight: (int) h;

-(int) width;

-(int) height;

-(int) area;

-(int) perimeter;


@end


@implementation Rectangle


-(void) setWidth: (int) w;

{

width = w;

}


-(void) setHeight: (int) h;

{

height = h;

}


-(int) width;

{

return width;

}


-(int) height;

{

return height;

}


-(int) area;

{

return height * width;

}


-(int) perimeter;

{

return 2 * height + 2 * width;

}


@end


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

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


Rectangle *aRectangle = [[Rectangle alloc] init];


[aRectangle setWidth: 6];

[aRectangle setHeight: 4];

NSLog(@"The dimensions of the rectangle are: %i in width, %i in height", [aRectangle width], [aRectangle height]);

NSLog(@"The area of the rectangle is: %i", [aRectangle area]);

NSLog(@"The perimeter of the rectangle is: %i", [aRectangle perimeter]);

[aRectangle release];

[pool drain];

return 0;

}


[Session started at 2009-03-30 02:12:09 +0100.]

2009-03-30 02:12:09.249 Ex7_Rectangle[1123:10b] The dimensions of the rectangle are: 6 in width, 4 in height

2009-03-30 02:12:09.250 Ex7_Rectangle[1123:10b] The area of the rectangle is: 24

2009-03-30 02:12:09.251 Ex7_Rectangle[1123:10b] The perimeter of the rectangle is: 20


The Debugger has exited with status 0.


8. Modify the add:, subtract:, multiply:, and divide: methods from Program 4.6 to return the resulting value of the accumulator. Test the new methods.


ANS - Copy and Paste of the Program and Console below.


#import


@interface Calculator: NSObject

{

double accumulator;

}


-(void) setAccumulator: (double) value;

-(void) clear;

-(double) accumulator;


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

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(void) subtract: (double) value;

{

accumulator -= value;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(void) multiply: (double) value;

{

accumulator *= value;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(void) divide: (double) value;

{

accumulator /= value;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


@end



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

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


Calculator *deskCalc = [[Calculator alloc] init];

[deskCalc clear];

[deskCalc setAccumulator: 100.0];

[deskCalc add: 200.];

[deskCalc divide: 15.0];

[deskCalc subtract: 10.0];

[deskCalc multiply: 5];

NSLog(@"The final result is: %f", [deskCalc accumulator]);

[deskCalc release];

[pool drain];

return 0;

}


[Session started at 2009-03-30 02:35:26 +0100.]

2009-03-30 02:35:26.990 Ex8_Calculator[1185:10b] The resulting value of accumulator is: 300.000000

2009-03-30 02:35:26.992 Ex8_Calculator[1185:10b] The resulting value of accumulator is: 20.000000

2009-03-30 02:35:26.992 Ex8_Calculator[1185:10b] The resulting value of accumulator is: 10.000000

2009-03-30 02:35:26.992 Ex8_Calculator[1185:10b] The resulting value of accumulator is: 50.000000

2009-03-30 02:35:26.993 Ex8_Calculator[1185:10b] The final result is: 50.000000


The Debugger has exited with status 0.


9. After completing exercise 8, add the following methods to the calculator and test them:

-(double) changeSign; // change sign of accumulator

-(double) reciprocal; // 1/accumulator

-(double) xSquared; // accumulator squared


ANS - Copy and Paste of @implementation, program and console of the above methods


@implementation Calculator


-(double) changeSign;

{

return -accumulator;

}


-(double) reciprocal;

{

return 1 / accumulator;

}


-(double) xSquared;

{

return accumulator * accumulator

@end



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

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


Calculator *deskCalc = [[Calculator alloc] init];

[deskCalc changeSign];

[deskCalc reciprocal];

[deskCalc xSquared];

NSLog(@"The final result is: %f", [deskCalc accumulator]);

[deskCalc release];

[pool drain];

return 0;

}


>>I was unable to get these methods to work and I am unsure as to why


10. Add a memory capability to the Calculator class from Program 4.6. Implement the following method declarations and test them:


-(double) memoryClear; // clear memory

-(double) memoryStore; // set memory to accumulator

-(double) memoryRecall; // set accumulator to memory

-(double) memoryAdd; // add accumulator to memory

-(double) memorySubtract; // subtract accumulator from memory


Have each method return the value of the accumulator.


ANS - Copy and Paste of @implementation, program and console of the above methods


@implementation Calculator


-(double) memoryClear;

{

memory = 0;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(double) memoryStore;

{

memory = accumulator;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(double) memoryRecall;

{

accumulator = memory;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(double) memoryAdd;

{

memory += accumulator;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


-(double) memorySubtract;

{

memory -= accumulator;

return NSLog(@"The resulting value of accumulator is: %f", accumulator);

}


@end


I have included the interface and implementation for this question but I have not included any methods in the main program as I could not resolve the problem in the previous question.


1 comment:

  1. Question 9:

    You have used the accumulator to return 'answers' but you have not actually carried out the task of modifying the accumulator itself. Perhaps why it is not working?

    Question 10:

    Is 'memory' declared in the interface?

    ReplyDelete