OxyCo

August 16, 2007

Programming issues. Writing a tax calculator: part 2.

Filed under: Objective C — atma @ 10:07 am

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.

August 15, 2007

Programming issues. Writing a tax calculator.

Filed under: Objective C — atma @ 9:56 pm

My Objective C reading doesn’t go as I would like to, but in a way proceeds. Today I forgot the ObjC manual in the military chamber, while going home, so I couldn’t read much. I decide to solve a simple self assigned exercise. Nothing complicated, it could be done in less than 4 seconds using Python or even C.
Anyway the good thing about this book is that, as Stephen Kochan explains, I’ll start programming using the specific object oriented programming mindset, avoiding some issues that more experienced programmers have when they try to learn objective C and object oriented programming. First, I’ll show you my code:

The header file

// ivacalc.h header

#import
#import

@interface TAX: Object
{
double price;
}

-(void) setPrice: (int) p;
-(double) price;

@end

The main program (implementation/main function)

// Calculating the tax of a give price v1.1
// main program
// ivacalc.m

#import
#import
#import “ivacalc.h”

@implementation TAX;

-(void) print
{
printf(”The price is %.2f and the tax is %.2f.\nThe sum is %.2f\n”, price, price * 0.19, price + (price*0.19));
}

-(double) price
{
return price;
}

-(void) setPrice: (int) p
{
price = p;
}

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

[myTax free];

return 0;
}

As you can see the program above is quite simple. It does not much. The hard part was defining correctly the interface and implementation sections and the inner goal is to get more familiar with methods and classes. However the first issue I’ve encountered was the data encapsulation. Which raises the question: What would have happened if I had put the scanf code inside the setPrice method? I could change the method, in order to make the method perform all the test + scanf execution internally. I tried to do it, I wrote the code but I` didn’t manage to get the print method accept a variable that is calculated from a method within the same class of the implementation section. I’m quite that this is possible though.
The problem is, which programming style is correct? When must I write a function or perform a specific operation inside the method and not on the main() function, as a C-style program, and then must I put the more possible options inside the method? Is there a universal answer or there are many, depending on the program?
I’ll continue writing code tomorrow! Bye

Blog at WordPress.com.