Convalesco

Growing stronger every day!

Archive for the ‘Objective C’ tag

Programming issues. Writing a tax calculator: part 2.

without comments

After playing around with the tax calculator I managed to write the zero or negative number control inside the method as I wanted before. It may seem too easy for expert programmers but for me was a bit tricky. First I used the while loop keyword and saw the printf line printing itself on the screen inside an infinite loop! Then I woke up and used the if/else statements.
The next problem was stopping the program from printing the lines that were at the main() function. The print method call, was printing 0’s anyway. So another if/else control on the print method was enough. I did change the print method, at the header file, in order to make it accept a single argument. I’m not sure if it was necessary for print to work as I wanted. Stop! Let’s test it! No! Here is the error: ‘p’ undeclared (first use in this function) which is quite normal, because the method doesn’t expect any variable or argument. However, I could try to declare the variable inside the method, but when I did that and try to compile the program, xcode crashed! Yeah! Am I good or what? Anyway, declaring inside a method a value that should be retrieved from outside doesn’t make much sense anyway.
So here is the code:

The .h file:


#import 
#import 

@interface TAX: Object
{
double price;
}

-(void) setPrice: (int) p;
-(void) print: (int) p;

@end

and the .m file:

#import 
#import 
#import "ivacalc.h"

@implementation TAX

-(void) print: (int) p
{
price = p;

if ( p > 0 )
{
printf("The price is %.2f and the tax is %.2f.\nThe sum is %.2f\n", price, price * 0.19, price + (price*0.19));
}
else
{
printf("exiting\n");
}
}

-(void) setPrice: (int) p
{
if (p < 1) // don't do calculation for negative numbers
{
printf("The price is 0 or negative!\n");
p = 0;
}
else
p = price;
}

@end

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

TAX *myTax = [[TAX alloc] init]; // allocating and initializing a TAX-fellow class!

printf("Price: ");
scanf("%i", &j);
printf("\n");

[myTax setPrice: j];
[myTax print: j];

[myTax free];

return 0;
}

Now the program does check if the number entered is 0 or negative and prints an error message if it is.

Written by Panagiotis Atmatzidis

August 16th, 2007 at 10:07 am

Posted in Uncategorized

Tagged with

Creating a Calculator Class

with 5 comments

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 
#import 

@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

Written by Panagiotis Atmatzidis

November 20th, 2006 at 5:46 pm

TextMate blogging & the Objective C book intro

with 2 comments

Following the this video I did not managed to post through TextMate the famous MacOSX all-in-one editor. Although the blog functionality seems quite elegant doesn’t work as it should.
First all when fetching post’s through wordpress.com it fetches my Dashboard articles and not my previous posts. I don’t know why, maybe Wordpress API is not working as it should? I’m quite positive that Allan’s blog should be a WordPress blog although from the video I didn’t understand if it’s WordPress or webpress, the spelling was not clear enough plus the fact I don’t speak native English, so I can’t be sure. He uses Markdown instead of HTML though. When I post I get error cannot post to this blog or category - (401). So if anyone reading this (Hello?) has managed to use TextMate with wordpress.com please let me know. Anyway..
I purchased from Amazon the following book: Programming in Objective-C by Stephen Kochan.
The first impression is good. The book seems to be what I needed in first place and has some positive feedback at Amazon’s website.

Jeez, writing in simple html for blog post’s in TextMate is much more easy then using other editors! Actually most of my posts were written in TextEdit which I like very much for it’s power and simplicity, but it’s not an HTML editor to make an acceptable comparison!

I noticed that most of the mac programs like the one’s already mentioned, QuickSilver, VoodooPad, etc. are made in a very smart, elegant, simple yet powerful way. It’s not just the operating system, it’s the entire community that has a genius mindset :-)
uhh!!
I need to get some sleep and read a bit about instances and methods!!!

Cheerz!

EDIT: Update! I’m editing through TextMate!

My mistake was putting the this line:

oxyCo http://atma@wordpress.com/xmlrpc.php

instead of this:

oxyCo http://atma@atma.wordpress.com/xmlrpc.php

Now seems to work perfectly! :-)

EDIT2: I just purchased TextMate. Hope it’s worth the 39$!!

Written by Panagiotis Atmatzidis

November 3rd, 2006 at 3:44 pm

Hello Objective C!

without comments

Hello Objective C!

This is my new blog, entirely dedicated in Objective C. I am a starter, never had experience with programming languages, although I was/am able to write small scripts in bash and / or python. Nothing fancy but enough to keep my GNU/Linux tuned!
So a Mac lover with a Linux background in the world of Objective C and object oriented programming! Today I wrote my first program:

Hello world

Surprise ha? :-)

The reason I choose Objective C and not C, Python, Ruby or something else is the cocoa framework which is the best environment I’ve ever seen. I bought 2 books, the first is an extensive guide into Objective C for starters - non programmers - and the second one is an introduction to cocoa framework! I hope that books will speed up the learning process. Due to my work I don’t very much free time but I’ll try to finish the book in a reasonable time.

Cya

[myObject Free];

Written by Panagiotis Atmatzidis

November 2nd, 2006 at 7:22 pm