473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why is my code not returning the correct value?

Hi everyone,

I am trying to program a Form Pay Estimator and it calculates the gross pay, taxes owed, and net pay for an individual employee. For some reason my CalculateTaxes method in my Pay() class is not returning the correct value. This is the code I have here:

In my Pay() class:
Expand|Select|Wrap|Line Numbers
  1. class Pay
  2.     {
  3.             //declare variables
  4.             private static int dependants;
  5.             private static double grossPay;
  6.  
  7.             //property that gets and sets the dependants
  8.             public int NumOfDependents
  9.             {
  10.                 get
  11.                 {
  12.                     return dependants;
  13.                 }
  14.                 set
  15.                 {
  16.                     dependants = value;
  17.                 }
  18.             }
  19.  
  20.         //calculates the gross pay for the production worker employee, using their hours
  21.         //worked times their wage and overtime is calculated for the employee, if applicable
  22.         public double CalculateGrossPay(double hoursWorked, double wageRate)
  23.         {
  24.             grossPay = hoursWorked * wageRate;
  25.                 if (hoursWorked > 40)
  26.                 {
  27.                     grossPay += (.5 * wageRate) * (hoursWorked - 40);
  28.                 }
  29.                 return grossPay;
  30.         }
  31.  
  32.         //calculates the gross pay for a salesperson, using their hours worked times their
  33.         //wage rate and then commission is calculated for the employee, based on their sales
  34.         //amount
  35.         public double CalculateGrossPay(double hoursWorked, double wageRate, double salesAmount)
  36.         {
  37.             grossPay = hoursWorked * wageRate;
  38.             if (salesAmount <= 10000)
  39.             {
  40.                 grossPay += .02 * salesAmount;
  41.             }
  42.             else if (salesAmount > 10000)
  43.             {
  44.                 grossPay += .04 * salesAmount;
  45.             }
  46.             return grossPay;
  47.         }
  48.  
  49.         //calculates the taxes the employee has to pay
  50.         public static double CalculateTaxes()
  51.         {
  52.             int payCutoff = 100 + (100 * dependants);
  53.  
  54.             if (grossPay <= payCutoff)
  55.             {
  56.                 double taxesPaid = .1 * grossPay;
  57.                 return taxesPaid;
  58.             }
  59.             else
  60.             {
  61.                 double taxesPaid = (.1 * payCutoff) + (.2 * (grossPay - payCutoff));
  62.                 return taxesPaid;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
And in my Form class:
Expand|Select|Wrap|Line Numbers
  1. public partial class FormPayEstimator : Form
  2.     {
  3.         public FormPayEstimator()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         //closes the application
  9.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  10.         {
  11.             Application.Exit();
  12.         }
  13.  
  14.         //closes the application
  15.         private void buttonExit_Click(object sender, EventArgs e)
  16.         {
  17.             Application.Exit();
  18.         }
  19.  
  20.         //clears all text boxes, unchecks the salesperson check box, makes the sales amount
  21.         //text box and label invisible and sets the focus back to the hours worked text box
  22.         private void buttonClearForm_Click(object sender, EventArgs e)
  23.         {
  24.             textBoxDependants.Text = "";
  25.             textBoxGrossPay.Text = "";
  26.             textBoxHourlyWageRate.Text = "";
  27.             textBoxHoursWorked.Text = "";
  28.             textBoxNetPay.Text = "";
  29.             textBoxTaxes.Text = "";
  30.             checkBoxSalesperson.Checked = false;
  31.             textBoxSalesAmount.Visible = false;
  32.             labelSalesAmount.Visible = false;
  33.             textBoxHoursWorked.Focus();
  34.         }
  35.  
  36.         //displays information about the program
  37.         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  38.         {
  39.             MessageBox.Show("Pay Estimator - Version 1.0", "Pay Estimator", MessageBoxButtons.OK, MessageBoxIcon.Information);
  40.         }
  41.  
  42.         //if the user checks the salesperson check box the sales amount text box and label
  43.         //become visible to the user and it sets the focus to the sales amount text box
  44.         private void checkBoxSalesperson_CheckedChanged(object sender, EventArgs e)
  45.         {
  46.             textBoxSalesAmount.Visible = true;
  47.             labelSalesAmount.Visible = true;
  48.             textBoxSalesAmount.Focus();
  49.         }
  50.  
  51.         //displays the font dialog box and allows user to change their font
  52.         private void fontToolStripMenuItem_Click(object sender, EventArgs e)
  53.         {
  54.             fontDialog1.Font = textBoxHoursWorked.Font;
  55.             fontDialog1.Font = textBoxHourlyWageRate.Font;
  56.             fontDialog1.Font = textBoxDependants.Font;
  57.             fontDialog1.Font = textBoxGrossPay.Font;
  58.             fontDialog1.Font = textBoxTaxes.Font;
  59.             fontDialog1.Font = textBoxNetPay.Font;
  60.             fontDialog1.Font = textBoxSalesAmount.Font;
  61.             if (fontDialog1.ShowDialog() != DialogResult.Cancel)
  62.             {
  63.                 textBoxHoursWorked.Font = fontDialog1.Font;
  64.                 textBoxHourlyWageRate.Font = fontDialog1.Font;
  65.                 textBoxDependants.Font = fontDialog1.Font;
  66.                 textBoxGrossPay.Font = fontDialog1.Font;
  67.                 textBoxTaxes.Font = fontDialog1.Font;
  68.                 textBoxNetPay.Font = fontDialog1.Font;
  69.                 textBoxSalesAmount.Font = fontDialog1.Font;
  70.             }
  71.         }
  72.  
  73.         //displays the color dialog box and allows user to change thei font color
  74.         private void colorToolStripMenuItem_Click(object sender, EventArgs e)
  75.         {
  76.             colorDialog1.Color = textBoxHoursWorked.ForeColor;
  77.             colorDialog1.Color = textBoxHourlyWageRate.ForeColor;
  78.             colorDialog1.Color = textBoxDependants.ForeColor;
  79.             colorDialog1.Color = textBoxGrossPay.ForeColor;
  80.             colorDialog1.Color = textBoxTaxes.ForeColor;
  81.             colorDialog1.Color = textBoxNetPay.ForeColor;
  82.             colorDialog1.Color = textBoxSalesAmount.ForeColor;
  83.             if (colorDialog1.ShowDialog() != DialogResult.Cancel)
  84.             {
  85.                 textBoxHoursWorked.ForeColor = fontDialog1.Color;
  86.                 textBoxHourlyWageRate.ForeColor = fontDialog1.Color;
  87.                 textBoxDependants.ForeColor = fontDialog1.Color;
  88.                 textBoxGrossPay.ForeColor = fontDialog1.Color;
  89.                 textBoxTaxes.ForeColor = fontDialog1.Color;
  90.                 textBoxNetPay.ForeColor = fontDialog1.Color;
  91.                 textBoxSalesAmount.ForeColor = fontDialog1.Color;
  92.             }
  93.         }
  94.  
  95.         //calculates the users total gross pay, their taxes owed and their net pay
  96.         private void buttonCompute_Click(object sender, EventArgs e)
  97.         {
  98.             //declares variables
  99.             string inValue;
  100.             double hours, rate, dependants, salesAmount, grossPay, taxes, netPay;
  101.  
  102.             //assigns variables to values user entered in the hours worked, hourly wage rate,
  103.             //and dependants text boxes
  104.             inValue = textBoxHoursWorked.Text;
  105.             hours = double.Parse(inValue);
  106.             inValue = textBoxHourlyWageRate.Text;
  107.             rate = double.Parse(inValue);
  108.             inValue = textBoxDependants.Text;
  109.             dependants = int.Parse(inValue);
  110.  
  111.             //creates an instance of the Pay class and runs the CalculateGrossPay method for
  112.             //the production workers
  113.             Pay p1 = new Pay();
  114.             grossPay = p1.CalculateGrossPay(hours, rate);
  115.  
  116.             //checks to see if the sales amount checkbox is checked and if it is, a value is
  117.             //assigned to the salesAmount text box, an instance of the pay class is created
  118.             //and the CalculateGrossPay method for a salesperson is run
  119.             if (checkBoxSalesperson.Checked == true)
  120.             {
  121.                 inValue = textBoxSalesAmount.Text;
  122.                 salesAmount = double.Parse(inValue);
  123.                 Pay p2 = new Pay();
  124.                 grossPay = p2.CalculateGrossPay(hours, rate, salesAmount);
  125.             }
  126.             //displays the answer in the Gross Pay text box
  127.             textBoxGrossPay.Text = String.Format("{0:c}", grossPay).ToString();
  128.  
  129.             //runs the CalculateTaxes method from the Pay class and displays the result in the
  130.             //taxes text box
  131.             taxes = Pay.CalculateTaxes();
  132.             textBoxTaxes.Text = String.Format("{0:c}", taxes).ToString();
  133.  
  134.             //calculates the net pay for an employee and displays the result in the net pay
  135.             //text box
  136.             netPay = grossPay - taxes;
  137.             textBoxNetPay.Text = String.Format("{0:c}", netPay).ToString();
  138.         }
  139.     }
  140. }
  141.  
When I compute the values I got $70 for taxes when it is only meant to be $40. Can anyone tell me why this is happening?
Apr 15 '11 #1

✓ answered by code green

What values are passed to
Expand|Select|Wrap|Line Numbers
  1. public static double CalculateTaxes() 
And what value is returned?
Expand|Select|Wrap|Line Numbers
  1. return taxesPaid; 

7 2475
code green
1,726 Expert 1GB
What values are passed to
Expand|Select|Wrap|Line Numbers
  1. public static double CalculateTaxes() 
And what value is returned?
Expand|Select|Wrap|Line Numbers
  1. return taxesPaid; 
Apr 15 '11 #2
VijaySofist
107 100+
Hi,

Can you please give the Input values here, for Which you got $70 as Output.


Regards
Vijay.R
Apr 15 '11 #3
I put the hours worked as 40, the wage rate at 10, and 4 as the number of dependents.
Apr 15 '11 #4
Rabbit
12,516 Expert Mod 8TB
Shouldn't this line:
taxes = Pay.CalculateTaxes();

be this:
taxes = p1.CalculateTaxes();
Apr 15 '11 #5
Yes, i think that would be the best solution. I am going to try to remove all the static and set then back to normal and change what taxes equals like the way you just described
Apr 15 '11 #6
That did not work. I am still getting $70 as the value of the taxes. I am still inputting the values of 40, 10, and 4
Apr 15 '11 #7
Rabbit
12,516 Expert Mod 8TB
You say you're passing in the hours worked, pay rate, and dependents. But you're not. You never pass in dependents into any function. Nor do you set it at any point for the class instance.

This means dependents will default to 0 and that would explain why you're getting $70.
Apr 15 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
3
by: Phoenix | last post by:
I am trying to print date headings over comments (as headings) I have a simple XML file : <comments> <comment id=1234 yyyymmdd="20041230" flag="Y">..text..</comment> <comment id=1309...
0
by: Epson Barnett | last post by:
I have a C# Web application I'm working on that loads a csv file on application start. The csv is an inventory file which is used to create a collection in the Application object. If the collection...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
8
by: Roy Gourgi | last post by:
Hi, I would like to able to able to return a value from a called function but also retain the loop counter in the called function the next time I call it again. TIA Roy
4
by: Earl T | last post by:
When I try to get the netscape version for version 7, I get the HttpBrowserCapabilities class returning the version as 5 and not 7. (see code and output below) CODE HttpBrowserCapabilities...
2
by: Nathan Sokalski | last post by:
I have a DropDownList that is returning the value from index 0 regardless of which item is selected. The code that I am using to test which index it is returning is: Label1.Text =...
3
by: maflatoun | last post by:
Hi, I must be losing my mind why is the following code returning wrong values? var d = new Date(); alert(d); -> Fri Apr 7 09:28:18 EDT 2006 alert("Hour:" + d.getHours()); -> 9...
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
11
Parul Bagadia
by: Parul Bagadia | last post by:
I m returning a value of pointer of a structure from one function to main; but whenever it comes in main it becomes NULL... i used another pointer in main and equated its value to returning pointer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.