Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Tax calculation program

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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
    Last edited by howardderamus1111; May 2nd, 2014 at 08:05 PM.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    163
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default 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
    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.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tax calculation program

    Quote Originally Posted by Parranoia View Post
    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
    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.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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;
    	}
    }
    Last edited by howardderamus1111; April 12th, 2014 at 04:44 PM.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    163
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Tax calculation program

    The only thing you could check is that barack and obama are actually whats being stored in the array.

    In your for loop just try adding a debugging message like
    System.out.println("First: " + theFirstName[i] + ", Last: " + theLastName[i]);

Similar Threads

  1. help in how to add code to work with income tax calculation program
    By jayokayo69 in forum Object Oriented Programming
    Replies: 3
    Last Post: January 21st, 2014, 02:14 PM
  2. Why wont my program calculate sales tax?
    By scholaryoshi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2012, 08:30 PM
  3. help with tax program???
    By JavaNewb3 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2011, 01:34 PM
  4. First, simple, two-classed tax program
    By rousseau in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 24th, 2011, 03:14 PM
  5. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 03:23 PM

Tags for this Thread