Hello I need help with this java tax assignment it is asking to Write the Java code needed to perform the following:• Calculate state withholding tax at 6.0%, and calculate fed-eral withholding tax at 25.0%.• Calculate dependent deductions at 2.0% of the employee’s salary for each dependent.

public class Payroll.Java

``````public static void main(String args[])
{
double salary = 1250.00;
double stateTax;
double federalTax;
double numDependents = 2;
double dependentDeduction;
double totalWithholding;
double takeHomePay;

// Calculate state tax here.

System.out.println("State Tax: \$" + stateTax);
// Calculate federal tax here.

System.out.println("Federal Tax: \$" + federalTax);
// Calculate dependant deduction here.

System.out.println("Dependents: \$" + dependentDeduction);
// Calculate total withholding here.

// Calculate take home pay here.
``````
3
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by Schol-R-LEA

What exactly is your question, how to calculate numbers that have a percentage?
Here's an example

Decrease the salary by 25%

salary = salary - (salary * (25/100))

It would evaluate to: (for the example say that the salary is 100

salary = 100 - (100*0.25)
salary = 100 - 25
salary = 75

Edited by Slavi

Just to clarify what Slavi said, you only need to have this line in the actual code:

``````salary = salary - (salary * (25.0  / 100.0));
``````

The other lines just describe the computation. Note also that I changed the numbers to have decimal points followed by a zero; this is because, in Java, if you divide one integer constant by another integer, it automatically divides the number as an `int` instead of a`float` or `double`. Adding the decimal point to the number forces it to use floating-point division instead.

Now, in this case, you could have written

``````salary = salary - (salary * 0.25);
``````

and gotten the same result; however, doing that would hard-code the 25% value into the program. What happens if, instead, you need to have different values for the percentage? That's why you probably would divide the percent value by 100.0 instead, so that you could deal with the percentages instead of the decimal equivalent.

Oh, and here's a small trick: there are versions of the assignment operator which combine assignment with an arithmetic operator, called the accumulate operators, which add to, subtract from, multiply by, or divide into the value and then assign it. For example,

``````sum += 2;
``````

adds two to `sum` and assigns the new value to `sum`. The practical upshot of this is that you can write line from before like this instead:

``````    salary -= salary * 0.25;
``````

It does the same thing, but it's a shortcut. Don't worry too much about it now, but just know about it as you might see it later at some point.

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.