Method not calculating properly

Associate
Joined
11 Jul 2009
Posts
1,318
Hoping someone here can help, writing a program in c# that does simple calculation too someone salary, Having a problem with a method where it will multiply two numbers but wont subtract the third, it just outputs 700, it won't take the 700 off the salary, if ya get me.


Code:
 class Employee
    {
        public string employeeName;
        public int employeeAge;
        public string employeeDepartment;
        public int employeeNumber;
        public int weeklyHours;
        public double taxRate;
        public int yearsOfService;
        public double monthlySalary;
        public double TotalYearlyIncome = 0;
        public int Months = 12;
        public double Taxpaid = 0;
       
        public Employee() {}

        public Employee(string inEmployeeName,int inEmployeeAge,string inEmployeeDepartment,int inEmployeeNumber,int inWeeklyHours,double inTaxRate,int inYearsOfService,double inMonthlySalary,int inMonths,double inTaxPaid)
        {
          employeeName = inEmployeeName;
          employeeAge = inEmployeeAge;
          employeeDepartment = inEmployeeDepartment;
          employeeNumber = inEmployeeNumber;
          weeklyHours = inWeeklyHours;
          taxRate = inTaxRate;
          yearsOfService = inYearsOfService;
          monthlySalary = inMonthlySalary;
          Months = inMonths;
          Taxpaid = inTaxPaid;
        }

        public void EmployeeDetails()
        {
           Console.WriteLine("Employee's Name             :"+employeeName);
           Console.WriteLine("Employee's Age              :"+employeeAge);
           Console.WriteLine("Employee's Department       :"+employeeDepartment);
           Console.WriteLine("Employee's Number           :"+employeeNumber);
           Console.WriteLine("Employee's Weekly Hours:    :"+weeklyHours);
           Console.WriteLine("Employee's Tax Rate         :"+taxRate);
           Console.WriteLine("Employee's Years of service :"+yearsOfService);
           Console.WriteLine("Employee's Monthly Salary   :" + monthlySalary + "\n");
         
       
        }

        public double CalYearlySalary(int Months, double monthlySalary)
        {
           
           
            return ( Months * monthlySalary);
       
        }

       public double TaxCalculation(double monthlySalary,double taxRate)
       {
         
           return (monthlySalary*taxRate);
         
       }

       public double PayAfterTax(double monthlySalary, double taxRate, double Taxpaid)
       {
         
           return(monthlySalary * taxRate) - Taxpaid;
     
       }
     
    }

}



Code:
 class Program
    {
        static void Main(string[] args)
        {
            Employee a1 = new Employee("Kevin O Sullivan",31,"Software Writer",5667,39,0.20,5,3500,12,0);
            a1.EmployeeDetails();
            Console.WriteLine("Your yearly Salarly is     :" + a1.CalYearlySalary(a1.Months, a1.monthlySalary) + "\n");
            Console.WriteLine("The amount of tax you pay every Month is    :"+a1.TaxCalculation(a1.monthlySalary, a1.taxRate)+"\n");
            Console.WriteLine("Your take home pay is: " + a1.PayAfterTax(a1.monthlySalary,a1.taxRate,a1.Taxpaid));

            Employee b2 = new Employee("Mary Murphy",28,"Software Writer",5442,39,0.20,6,3600,12,0);
            b2.EmployeeDetails();

            Employee c3 = new Employee("John Cusack", 35, "Software Tester", 5876, 39, 0.20, 10, 4000,12,0);
            c3.EmployeeDetails();

            Employee d4 = new Employee("Teresa O Reilly",30,"Software Tester",5798,39,0.20,7,3800,12,0);
            d4.EmployeeDetails();

            Employee e5 = new Employee("Timmy Jones",35,"Payroll",6723,39,0.42,8,4200,12,0);
            e5.EmployeeDetails();

            Employee f6 = new Employee("Ciara O Mahony",40,"Head Accountant",4432,50,0.42,15,6000,12,0);
            f6.EmployeeDetails();

            Employee g7 = new Employee("Chris Murphy",43,"Technical Writer",3445,40,0.42,12,4500,12,0);
            g7.EmployeeDetails();


            Employee h8 = new Employee("Maire Higgins",49,"CEO/Manager",4214,55,0.42,25,7000,12,0);
            h8.EmployeeDetails();

       
       
       
       
       
       
       
       
       
       
        }
    }
}


The problem is in the method PayAfterTax, any help appreciated.
 
Lets say I have a method that adds two numbers,How would I pass the answer too another method too do another calculation with that answer? is it even possible too do it?

Code:
public int Calculate(int num1, int num2);

{
           return answer = num1 +num2;
      
}
 
Anyone tell me why this won't work properly?

Code:
   static void BankBalance()
        {
            int isValid = 0;
            do
            {
                try
                {

                    Console.WriteLine("Please enter your Bank Balance amount");
                    int amount = int.Parse(Console.ReadLine());

                      if (amount == 0)
                     {
                         Console.WriteLine("Thank you and goodbye");
                         break;
                     }

                     else if(amount >= 1 || amount < 100)
                    {
                        Console.WriteLine("You're very poor,are you sure you're not laundering money?");
                    }
                    else if(amount == 100 || amount < 200)
                    {
                        Console.WriteLine("Hmmm, really that's all you have!");
                    }
                    else if  (amount >= 200 || amount < 300)
                    {
                        Console.WriteLine("Ok your balance is beginning too look nice!");
                    }
                    else if (amount >=  300 || amount < 400)
                    {
                        Console.WriteLine("OK HAVE YOU A SECRET STASH!!!!");
                    }
                    else if (amount >= 500)
                    {
                     Console.WriteLine("Your rich *******!");
                    }
                    

                }
                catch (FormatException fe)
                {
                    Console.WriteLine("Please enter digits only!");
                }

            } while (isValid == 0);

It will exit the program when you press 0 and when you type a value greater than 1 or less than 100 it prints out the first line but if you type in any other value it won't print the of the console writeline statments, any ideas?
 
|| means OR

so this is always executed if its >= 1

else if(amount >= 1 || amount < 100)
{
Console.WriteLine("You're very poor,are you sure you're not laundering money?");
}

Should be &&

You should probably use a "Switch" statement instead

You are also missing some value ranges, such as between 400 and 499

Damn, looks like I need too learn my operators better :-| I was thinking switch might be better alright.

Thanks for the help.
 
To all of the people using double for monetary values, don't! Decimal is much more suitable.

Really? I've never seen used much, I'll be doing the Microsoft exam for c# in two weeks and it was mentioned in that once but our tutor never mentioned it when he was teaching us variable types!
 
I'm beginning too think I'm not cut out too be a programmer if I can't get a simple program like below too work the way I want it too, I just want the program too exit the loop and quit is typed but instead it continues on asking the second question, any help appreciated.


Code:
 static void Main(string[] args)
        {
            Choice();
        }


        public static void Choice()
        {


            string quitter = "quit";
           

            do{

                Console.WriteLine("Please enter your  first name: ");
                string name = Console.ReadLine();

                Console.WriteLine("Please enter your second name: ");
                string secondName = Console.ReadLine();


                if (quitter.ToLower() == "quit")
                {
                    Console.WriteLine("Goodbye");
                    break;
                }

             
           
           
           
            } while (quitter != "quit") ;
        }
 
I'm curious, did you actually attach a debugger at any point and walk through the code? It seems like if you get stuck after coding you don't know how to debug.

Also, please change your default 'to' to 'to' instead of 'too'.

What do you mean "attach a debugger"?
 
Last edited:
Back again with another problem, I have two methods that add numbers, I want the first to add two numbers and the second to also add two numbers but I want too pass the answer from the first method too 2nd method thus adding three numbers, any ideas?


Code:
  static void Main(string[] args)
        {
          int number = addtwonumbers(10,20);
          Console.WriteLine("The sum of the two numbers is"+number);
         
         int numbertwo = addthreenumbers(30,40);
         Console.WriteLine("The sum of the three number is now "+number,+numbertwo);
         
        }
        public static int addtwonumbers(int numone, int numtwo)
        {
            int number = numone + numtwo;

            return number;

        }

        public static int addthreenumbers(int numthree, int numfour)
        {

            int number = numthree + numfour;

            return number;
       
       
        }
 
I haven't done any serious coding for decades but two things leap out at me about your code: first, there's no inline documentation; and second, there's no error-checking. Has your tutor mentioned things like preconditions and post-conditions? Yes the code is trivial but you should start good practice right away.

Inline documentation meaning commenting code yes? that's down too me being lazy more than anything, I know I should do it,as for preconditions and postconditions, I've never heard them being mentioned.
 
Stop typing 'too' when you mean 'to'.

Code:
static void Main(string[] args)
{
    var result = addtwonumbers(10, 20);
    //output result here
  
    result = addthreenumbers(result, 30, 40);
    //output result here
}

public static int addtwonumbers(int val1, int val2)
{
    return va1 + val2
}

public static int addthreenumbers(int val1, int val2, int val3)
{
    return val1 + val2 + val3;
}

Was messing around with the code you posted and think I finally have the hang of it, cheers.

Code:
    int result = addtwonumbers(10, 20);
            //output result here

            result = addthreenumbers(result, 30, 40);
            //output result here

            Console.WriteLine("The total amount is: "+result);

            int resultTwo = addfournumbers(600,600,result);
            
            resultTwo = addfournumbers(result,600,600);

            Console.WriteLine("The new total amount is: "+resultTwo);

            int resultThree = addfivenumbers(1000, 1000, result, resultTwo);

            resultThree = addfivenumbers(1000, 1000, result, resultTwo);

            Console.WriteLine("The newest total is now: "+resultThree);


        
        }


        public static int addtwonumbers(int val1, int val2)
        {
         return val1 + val2;
        }

        
        public static int addthreenumbers(int val1, int val2, int result)
        {
            return val1 + val2 + result;
        }

        
        public static int addfournumbers(int val1, int val2, int result)
        {
            return val1 + val2 + result;
        }     
        
        
        public static int addfivenumbers(int val1,int val2,int result,int resultTwo)

        {
            return val1 + val2 + result + resultTwo;
 
Cheers for all the help,much appreciated! I had too take a step back from that program I was trying too write last as I just confused myself :confused: I'll come back too it again.

Here's a little program I wrote today which compares strings

Code:
string[] name=new string[5];

            name[0] = "kevin";
            name[1] = "bill";
            name[2] = "Ken";
            name[3] = "Bob";
            name[4] = "Jo";
           

            Console.WriteLine("Please enter your name: ");
            string input = Console.ReadLine();

            if (input.Equals("kevin"))
            {
                Console.WriteLine("Helllooo KEV MON!");
            }
            else
            {
                Console.WriteLine("WRONG NAME DUDE!");
            }

            Console.WriteLine("Please enter another name:");
            string input2 = Console.ReadLine();

            if (input2.Equals("bill"))
            {
                Console.WriteLine("Helllloooooooo Billy Boy");
            }
            else
            {
                Console.WriteLine("CRAPPY NAME DUDE!");
            }

Nothing too exciting LOL! but at least it works, its not finished btw.
 
I mean did you actually do any proper debugging or are you perpetually guessing without observing. I think your response is pretty damning of whatever course you're doing if you don't know what 'attach a debugger' means. Debugging is essential to programming - learn to do it asap!

Our tutor never mentioned "debugging" when we would run our programs we were told too click "debug,run without debugging"
 
I passed my course :) and will be starting a mobile technology course in January :).

Seeing as we'll be using Java for that course, I decided to start messing around with it a bit using eclipse. I'm confused about something though, "public static class Hero "why do I have to put in the static? If I don't put it in Warrior Kevin = new Warrior(); gets an error.

Code:
public class Inheritence5 {

    public static class Hero
    {
        protected String Name;
        protected int Age;
        protected String Class;
        protected String Vocation;
        protected int Strenght;
        protected int DamageOutPut;
        protected int HitPoints;
        protected int DefensePoints;
        protected int Intelligence;
        protected int Focus;
        protected Boolean Evil;
        protected Boolean Good;
    
    public  void HeroStats()
    {
         System.out.println("Name: " +Name);
         System.out.println("Age: " +Age);
         System.out.println("Class " + Class);
         System.out.println("Vocation: " +Vocation);
         System.out.println("Strenght: " +Strenght);
         System.out.println("Damage Output: " +DamageOutPut);
         System.out.println("HitPoints: " +HitPoints);
         System.out.println("DefensePoints: " +DefensePoints);
         System.out.println("Focus: " +Focus);
         System.out.println("Evil: " +Evil);
         System.out.println("Good: " +Good);
    }
    
    }
    
    public  static class Warrior extends Hero
    {
         public int Stamina;
         public double CriticalStrike;
        
         public void SpecialStat()
         {
             System.out.println("Stamina: " +Stamina);
             System.out.println("CriticalStrike" +CriticalStrike);
         }
    }
    
    public static class Wizard extends Hero
    {
        protected int MagicalPoints;
        
        
        protected void SpecialStats()
        {
            System.out.println("MagicalPoints: " +MagicalPoints);
            
        }
    }
    
    
    public static void main(String[] args)
    {
        Warrior Kevin = new Warrior();
        Kevin.Name = "Kevin";
        Kevin.Age = 31;
        Kevin.Class = "Warrior";
        Kevin.Vocation = "Tank";
        Kevin.Strenght = 90;
        Kevin.DamageOutPut = 200;
        Kevin.HitPoints = 350;
        Kevin.DefensePoints = 100;
        Kevin.Intelligence = 50;
        Kevin.Focus = 56;
        Kevin.Evil = false;
        Kevin.Good = true;
        Kevin.Stamina = 78;
        Kevin.CriticalStrike = 77.5;
        Kevin.HeroStats();
        Kevin.SpecialStat();
      
        System.out.println();
        
         Wizard Conor = new Wizard();
         Conor.Name = "Conor";
         Conor.Age = 32;
         Conor.Class = "Wizard";
         Conor.Vocation = "Healer";
         Conor.Strenght = 40;
         Conor.DamageOutPut = 300;
         Conor.HitPoints = 500;
         Conor.DefensePoints = 100;
         Conor.Intelligence = 200;
         Conor.Focus = 80;
         Conor.Evil = false;
         Conor.Good = true;
         Conor.MagicalPoints = 500;
         Conor.HeroStats();
         Conor.SpecialStats();
        
         System.out.println();

I wrote more or less the same program in visual studio with c# and didn't have this problem at all with static, Is there a way to fix it?
 
Back
Top Bottom