# Tax calculation program

• April 9th, 2014, 09:53 PM
howardderamus1111
Tax calculation program
You MUST: use arrays to store taxpayer information. Use methods for tasks that will be repeated.

Ask the user how many taxpayers he would like to calculate taxes for.
Ask the user to enter each taxpayer's first name, last name gross income, and number of children.
Each taxpayer's tax due is computed as follows
The taxpayer's dependency exemption is determined by multiplying \$3,000 times the number of children.
The taxpayer's net income is determined by taking the taxpayer's gross income and subtracting the taxpayer's dependency exemption.
If the taxpayer's net income is between 0 and 50,000, the tax due is 15% of net income.
If the taxpayer's net income is greater than 50,000, the tax due is
15% of the first 50,000 of net income PLUS
25% of any income over 50,000
The tax due can never be less than 0.
If the net income is a negative number, the tax due is 0
TAXPAYER INFORMATION: output a message in one dialog box which lists the following info for every taxpayer: first name, last name, gross income, number of children, tax due.
AVERAGE TAX: output a message in a dialog box which states the average of the taxes due.
PRESIDENTAL MESSAGE: output a message in a dialog box which says either "We computed taxes for the president. " or "We did not compute taxes for the president." The president's name is Barack Obama.

Here is my code so far

Code java:

```import javax.swing.JOptionPane; public class AssignmentSeven { public static void main (String [] args) { String [] taxPayers; String[] firstName; String [] lastName; String message = ""; double[] grossIncome; double [] netIncome; int children; int taxPayersSize; double dependencyExemption; double[] taxDue; taxPayersSize = Integer.parseInt (JOptionPane.showInputDialog (null, "How many tax payers do you want to calculate? ")); taxPayers = new String [taxPayersNumbers]; firstName = new String [taxPayersNumbers]; lastName = new String [taxPayersNumbers]; grossIncome = new double [taxPayersNumbers]; netIncome = new double [taxPayersNumbers]; taxDue = new double [taxPayersNumbers]; for (int i=0; i<taxPayers.length; i++) { firstName [i] = JOptionPane.showInputDialog (null, "What is your first name? "); lastName [i] = JOptionPane.showInputDialog (null, "What is your last name? "); grossIncome[i] = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income? ")); children = Integer.parseInt(JOptionPane.showInputDialog(" How many children do you have? ")); dependencyExemption = 3000*children; netIncome [i] = grossIncome[i] - dependencyExemption; taxPayers[i] = "\n First name: " + firstName [i] + " \n Last name: " + lastName [i] + "\n Gross Income: " + grossIncome [i] + "\n Number of children: " + children; taxDue [i] = calculateTax (netIncome [i]); message += taxPayers[i] + "\n"; } JOptionPane.showMessageDialog (null,message); JOptionPane.showMessageDialog (null, "Hello, my name is Howard Deramus."); } public static double calculateTax (double [] theNetIncome) { double [] theTaxDue= new double [0]; double tax1 =0.15; double tax2 =0.25; for (int i= 0; i<theNetIncome.length; i++) { if (theNetIncome [i] > 50000) { theTaxDue[i] = (tax1*50000) + (tax2 *(theNetIncome[i]-50000)) ; } else if (theNetIncome[i] > 0) { theTaxDue[i] = (tax1*theNetIncome [i]); } else { theTaxDue [i] = 0; }//end else return theTaxDue [i]; } }   }```

And i get this error.

error: method calculateTax in class AssignmentSeven cannot be applied to given types;
taxDue [i] = calculateTax (netIncome [i]);
^
required: double[]
found: double
reason: actual argument double cannot be converted to double[] by method invocation conversion
1 error

Tool completed with exit code 1

Any help would be greatly appreciated. Thank you
• April 10th, 2014, 01:46 AM
Parranoia
Re: Tax calculation program
That error is because you are passing a double to the method rather than an array of doubles.
Looking at your code you might be a bit confused as to where you might need to use arrays and how to properly use them.

In your calculateTax method you probably want to pass in a double rather than a double array. Also, this declaration doesn't make much sense
Code java:

`double [] theTaxDue = new double[0];`
Basically what this line is doing is creating an array of doubles of length 0, so it's an empty array.

And since you won't be passing in an array, you don't need the for loop in there either.
• April 10th, 2014, 06:39 AM
howardderamus1111
Re: Tax calculation program
Quote:

Originally Posted by Parranoia
That error is because you are passing a double to the method rather than an array of doubles.
Looking at your code you might be a bit confused as to where you might need to use arrays and how to properly use them.

In your calculateTax method you probably want to pass in a double rather than a double array. Also, this declaration doesn't make much sense
Code java:

`double [] theTaxDue = new double[0];`
Basically what this line is doing is creating an array of doubles of length 0, so it's an empty array.

And since you won't be passing in an array, you don't need the for loop in there either.

So how do you suppose I should do it? I just started learning java and arrays I don't get them that good.
• April 12th, 2014, 03:35 PM
howardderamus1111
Re: Tax calculation program
I did what you said and thank you. It works now. But now I have a different problem. I have a different method which verifies if the first name is "Barack" and last name is "Obama". If the first and last name matches then it says " We calculated taxes for the president." If it doesn't match then it says "We did not compute taxes for the president. The president's name is Barack Obama". Even if the first and last name matches with Barack and Obama respectively, it still displays the latter message. Here is the upda

Code java:

```import javax.swing.JOptionPane; public class Assignment7 { public static void main (String [] args) { String[] firstName; String [] lastName; String message = ""; double[] grossIncome; double [] netIncome; int numberOfChildren; int taxPayersSize; double dependencyExemption; double[] taxDue; taxPayersSize = Integer.parseInt (JOptionPane.showInputDialog (null, "How many tax payers do you want to calculate? ")); firstName = new String [taxPayersSize]; lastName = new String [taxPayersSize]; grossIncome = new double [taxPayersSize]; netIncome = new double [taxPayersSize]; taxDue = new double [taxPayersSize]; for (int i=0; i<taxPayersSize; i++) { firstName [i] = JOptionPane.showInputDialog (null, "What is your first name? "); lastName [i] = JOptionPane.showInputDialog (null, "What is your last name? "); grossIncome[i] = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income? ")); numberOfChildren = Integer.parseInt(JOptionPane.showInputDialog(" How many children do you have? ")); dependencyExemption = 3000*numberOfChildren; netIncome [i] = grossIncome[i] - dependencyExemption; taxDue [i] = calculateTax (netIncome [i]); message += "\n First name: " + firstName [i] + " \n Last name: " + lastName [i] + "\nGross Income: " + grossIncome [i] + "\nNumber of children: " + numberOfChildren + "\nTax Due: " + taxDue[i] + "\n"; } JOptionPane.showMessageDialog (null,message); JOptionPane.showMessageDialog (null, averageTax (taxDue)); JOptionPane.showMessageDialog (null, presidentialMessage (firstName , lastName));   } public static double calculateTax (double theNetIncome) { double theTaxDue; double tax1 =0.15; double tax2 =0.25; if (theNetIncome > 50000) { theTaxesDue = (tax1*50000) + (tax2 *(theNetIncome-50000)) ; } else if (theNetIncome > 0) { theTaxDue = (tax1*theNetIncome ); } else { theTaxDue = 0; } return theTaxDue ; } public static String averageTax ( double [] theTaxesdue1) { double average = 0; double sum = 0; String message1 = ""; for (int i=0;i<theTaxdue1.length; i++) { sum = sum + theTaxdue1 [i]; average = sum/ (theTaxdue1.length); } message1 += "The average tax due is " + String.format ("%.2f", average);   return message1; } public static String presidentialMessage (String [] theFirstName , String [] theLastName ) { String message2 = ""; for(int i = 0; i < theFirstName.length; i++) { if (theFirstName[i].equalsIgnoreCase("Barack") && theLastName[i].equalsIgnoreCase("Obama")) { message2 = "We computed taxes for the president."; } else { message2 = "We didn't compute tax for the president. The president's name is Barack Obama."; } } return message2; } }```
• April 12th, 2014, 05:44 PM
Parranoia
Re: Tax calculation program
The only thing you could check is that barack and obama are actually whats being stored in the array.

`System.out.println("First: " + theFirstName[i] + ", Last: " + theLastName[i]);`