2
\$\begingroup\$

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:

  1. Create a new project in NetBeans called Payrollimplement
  2. Create a new class in the same project called PayrollCal
  3. 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.
  4. 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();


    }

}
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving the cleanliness of existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! \$\endgroup\$ – Simon Forsberg Dec 18 '14 at 13:40
  • \$\begingroup\$ To give you a small push on the way: There's a big difference between a static method and a non-static method. \$\endgroup\$ – Simon Forsberg Dec 18 '14 at 13:41
  • \$\begingroup\$ Sorry, André Stannek commented on a Question i put on the wrong page and he put a link for here on it. Thanks for the small push tho, will get back to my notes and try figure it out. tkanks Simon. \$\endgroup\$ – SwishGavigan18 Dec 18 '14 at 13:45
  • 3
    \$\begingroup\$ "In this class you will have four methods" -- does your class have those four methods? Looks like you're missing the bonus calculation method. \$\endgroup\$ – Edward Dec 18 '14 at 14:09
  • 2
    \$\begingroup\$ @Edward Yes, his class has exactly four methods. The one called returnPay calculates the bonus, and with it serving double duty :D sort of everything is covered by mere three methods which made one free for main. :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\$ – maaartinus Dec 19 '14 at 2:09
2
\$\begingroup\$

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, and
  • getBonus

Now we need to decide the return type and parameters for these methods.

  • setPay should have one parameter, pay, which is a double. It does not return a value, so it has type void.
  • getPay should return a double and has no parameters:.
  • getTakeHomePay returns a double and has one parameter, a double named tax.
  • 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.

\$\endgroup\$
  • \$\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 Dec 19 '14 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.