I have written the code as best that I know how but I am unsure that the questions asked are being fulfilled. Any advice to improve this code would really help me.
Question asked:
- Create a new project in NetBeans called
Payrollimplement
- Create a new class in the same project called
PayrollCal
In this class you will have four methods:
- The first method will set pay
- The second method will return the pay
- The third method will calculate take home pay (pay-tax which will be passed onto the method)
- The fourth method will calculate a bonus which is 10% int this case.
In your main class create a
PayrollCal
object and invoke your methods to ensure they work.
package payrollcal;
/**
*
* @author ace
*/
public class PayrollCal {
public static void setPay()
{
double setPay = 400;
double tax1Pay = setPay;
System.out.println("Wages are:\t"+setPay);
System.out.println("Wages + Bonus are:\t"+(returnPay(setPay)+setPay));
System.out.println("Tax Payed on salary:\t"+(TaxPay(tax1Pay)));
System.out.println("Total salary :\t"+((returnPay(setPay))+setPay+-(TaxPay(tax1Pay))));
}
public static double returnPay(double setPay)
{
return (setPay *.1);
}
public static double TaxPay(double tax1Pay)
{
double netPay = 400*.1;
double tax = netPay/.24;
double taxPay;
tax1Pay = netPay - (netPay/tax);
return (tax1Pay);
}
public static void main(String[] args)
{
setPay();
}
}
1 Answer 1
Let's take this step-by-step
- The first method will set pay
- The second method will return the pay
- The third method will calculate take home pay (pay-tax which will be passed onto the method)
- The fourth method will calculate a bonus which is 10% int this case.
First we choose names for the four methods. I'm going to go with
setPay
getPay
getTakeHomePay
, andgetBonus
Now we need to decide the return type and parameters for these methods.
setPay
should have one parameter,pay
, which is adouble
. It does not return a value, so it has typevoid
.getPay
should return adouble
and has no parameters:.getTakeHomePay
returns a double and has one parameter, adouble
namedtax
.getBonus
returns a double and has no parameters.
Now we have the skeleton of our class
class PayrollCal {
public void setPay(double pay) {
}
public double getPay() {
}
public double getTakeHomePay(double tax) {
}
public double getBonus() {
}
}
Let's start at the top, with setPay
. We need an instance variable pay
of type double
.
class PayrollCal {
private double pay;
public void setPay(double pay) {
this.pay = pay;
}
...
The other methods I will leave up to you.
-
\$\begingroup\$ Thanks very much for showing me the right way to go about the code. Spent a good while studying last night to try clean the code up and this is what i got(Under old code above). I am going to try it again now with what you have shown me. Thanks again. \$\endgroup\$SwishGavigan18– SwishGavigan182014年12月19日 10:02:59 +00:00Commented Dec 19, 2014 at 10:02
returnPay
calculates the bonus, and with it serving double duty :D sort of everything is covered by mere three methods which made one free formain
. :D I'm voting to reopen, as it's not exactly broken and can and should be reviewed. By applying a few common principles the mess can be cleaned up, so let's teach him. \$\endgroup\$