In this tutorial, we will take a look at how to make a more accurate pay program using the tax rates. Now I'm not sure if it's the same for every state, but I'm just letting you know, I will be working off of the New Jersey tax rates.
First as usual, we would need the imports:
CODE
import java.util.*;
Next we need to set up the class:
CODE
public class javaPay { //name it whatever you wish, but you have to name the .java file the same thing.
Now, we will be using a method to return the final pay:
CODE
public class javaPay {
public static double finalPay(double netPay){
double finalPay=0;
//This section might vary for other states/countries, just be aware of this.
if(netPay<=5000){
finalPay=(netPay-(netPay*.08));
}
if(netPay>=5000.01&&netPay<=10000&&netPay>5000){
finalPay=(netPay-(netPay*.10));
}
if(netPay>=10000.01&&netPay>10000){
finalPay=(netPay-(netPay*.12));
}
return finalPay;
}
Now we need the driver function which will run the program:
CODE
public static void main (String args[]) {
double netPay,totalPay,hours; //variables used to calculate pay
Scanner fox=new Scanner(System.in); //input device since JAVA doesn't use cin>>
System.out.print("Enter net pay: ");
netPay=fox.nextDouble();
System.out.print("Enter hours: ");
hours=fox.nextDouble();
netPay*=hours; //calculates pay before taxes
totalPay=finalPay(netPay); //uses our method to calculate pay after taxes
System.out.println("Before taxes: "+netPay); //outputs final results
System.out.println("After taxes: "+totalPay);
How will this look when we put the code together?
CODE
import java.util.*;
public class javaPay {
public static double finalPay(double netPay){
double finalPay=0;
if(netPay<=5000){
finalPay=(netPay-(netPay*.08));
}
if(netPay>=5000.01&&netPay<=10000&&netPay>5000){
finalPay=(netPay-(netPay*.10));
}
if(netPay>=10000.01&&netPay>10000){
finalPay=(netPay-(netPay*.12));
}
return finalPay;
}
public static void main (String args[]) {
double netPay,totalPay,hours;
Scanner fox=new Scanner(System.in);
System.out.print("Enter net pay: ");
netPay=fox.nextDouble();
System.out.print("Enter hours: ");
hours=fox.nextDouble();
netPay*=hours;
totalPay=finalPay(netPay);
System.out.println("Before taxes: "+netPay);
System.out.println("After taxes: "+totalPay);
}
}
And if we try it out with the following values:
Pay per hour: 20
Hours: 60 (Total hours getting paid for, be it for one week or two)
Your should get:
I hope that was helpful and I hope you take a look at my other stuff.