OxyCo

November 20, 2006

Creating a Calculator Class

Filed under: Objective C — atma @ 5:46 pm

Creating a calculator class in Objective C is not an easy task! Oh, c’mon sure it ;-) . I’ve been reading about operators lately. Arithmetic operators, bit operators, assignment operators etc. The fact that many of these things were known helps me gap the lack of time to proceed with reading as I wanted too. Here is my last code:

	#import <objc/Object.h>
	#import <stdio.h>

	@interface Calculator: Object
	{
		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, char *argv[])
	{
		Calculator *calc;

		calc = [[Calculator alloc] init];

		[calc clear];
		[calc setAccumulator: 100.00];
		[calc add: 200.00];
		[calc divide: 15.00];
		[calc subtract: 10.00];
		[calc multiply: 5.00];

		printf(”The result is %g\n”, [calc accumulator]);
		[calc free];

		return 0;
	}
	

Okay, pretty simple you’ll say! But it’s not! Actually for the first time some things came natural! The allocation and initialisation of memory, I didn’t even think about the right syntax after the first line [calc clear]; which helped write the program quickly. However I made some mistakes the first I wrote the program. The first one was not paying attention that the accumulator was of type “double” and not “void” which caused some warning but not fatal errors

5 Comments »

  1. Hmmm. I don’t think what you have there actually compiles, let alone runs without errors, at least on Apple’s implementation of Objective-C. What kind of compiler/libraries did you use for this? And for which version of Objective-C?

    For starters while you’re extending Object. NextStep/Cocoa defines NSObject. Then you do [calc free]. The correct way to release an object is by [object release], or — this is not recommended — [object dealloc]. Perhaps you were thinking of C?

    Good to see more people coding in Objective-C. Good luck.

    Comment by cosmix — November 23, 2006 @ 9:07 pm

  2. Hello there Cosmix!

    Actually the #import was naked because completelly missing html tags, I didn’t notice that the < and > characters needed special html tags to be displayed.

    Now compiles at least via command line:

    OSX atma ~ $ file calculator.m
    calculator.m: ASCII C program text
    OSX atma ~ $ gcc calculator.m -o calculator -l objc
    OSX atma ~ $ ./calculator
    The result is 50
    OSX atma ~ $

    As for [calc free], I’ve read in the manual that it’s the a way to release the allocated & initialized memory. I didn’t knew that there is another way to do it and I don’t have a clue about the differences. I’ll try out the [object release]; method asap!

    Thnx for the tips :-)

    Comment by atma — November 24, 2006 @ 11:58 am

  3. Ah, you were using ‘pure’ Objective-C. Apologies, I was referring to Cocoa (or at least Foundation). You see, even with simple command line tools, it’s a good idea to leverage the features provided by the Foundation classes.

    You won’t find release and dealloc in the ‘pure’ objc runtime, as they are part of NSObject. At some point you will essentially be forced to import at least Foundation to do any sort of meaningful work. I’d suggest you spend some time familiarising yourself with NSObject, as opposed to the pure ObjC ‘Object’ as you probably won’t be using the latter too much.

    Comment by cosmix — November 24, 2006 @ 1:51 pm

  4. I didn’t knew the differences thank you for the enlightment :-) I’ll keep that in mind. For now I do mostly exercises that the book shows.

    I’m dealing with general purpose concepts. However the main idea is to be able to write cocoa applications sooner of later.. so your tips are very useful.

    Comment by atma — November 24, 2006 @ 2:09 pm

  5. beautiful online information center. greatest work… thanks

    Comment by bobbyRicky — February 22, 2007 @ 8:12 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.