tags:

views:

332

answers:

2

Hi I'm Having a logical error

Now I provided the following as input

the Salary i input the salary 30000

the No. Child 9

so the so the net salary will be

the family bonus + salary - tax
     (750)       + (30000) - (3000)

but my program count them as 
      (1500)    + (30000) + (6000)

my program doubled ( accumulate ) the family bonus and the tax

I hope I explain right

A: 

Another problem seems to lie in the fact that you don't initialize familybonus when you say familybonus += 300. So everytime you call GetFamilybonus it's added to the previous result. You call it twice in the PrintEmployee function, once directly and once indirectly by calling getNet;

Glenner003
+2  A: 

I took the existing code, and hard-wired the inputs (rather than using Console.ReadLine()), I get:

You are 28 years old Salary= 30000 Tax= 3000 Family bonus= 750 Net= 25500

The main problem seems to be not initializing values - i.e. treating fields as variables:

public double getTax()
{
    if (Salary < 10000)
        tax = 0;
    if (Salary <= 10000 && Salary >= 20000)
        tax += Salary * 0.05;
    else tax += Salary * 0.1;
    return tax;
}

OK - and what does tax start at if Salary >= 10000, etc. Likewise familyBouns in getFamilyBonus. By the way, how can Salary be both <= 10000 and >= 20000?

To illustrate, I've changed the output to:

    Console.WriteLine("Tax= {0}", getTax());
    Console.WriteLine("Tax= {0}", getTax());
    Console.WriteLine("Tax= {0}", getTax());

Which shows:

Tax= 3000 Tax= 6000 Tax= 9000

My advice would be: don't store calculated values unless you know the math is so complex that it is worth it. Just calculate them as needed (no field at all).

Marc Gravell
yes and thats wrong the net should be 27750
@john - I've added detail about that...
Marc Gravell
thanx a lot marc that solved the problem
@john - see also the bit I've highlighted in bold
Marc Gravell