I am having trouble understanding exactly what I am supposed to do with the following instructions.

a. Create a class named Pay that includes variables of type double that hold rate of pay per hour and withholding rate percent. Gross pay is computed as hours worked times rate of pay per hour. Net pay is calculated as gross pay minus (gross pay times withholding rate). For example, if the withholding rate is 25% and gross pay is $100, then net pay would be $100 - (100 * .25).

b. Create two overloaded methods named computeNetPay(), each of these methods will take different parameters and will calculate net pay based . These methods are to return the calculated value which will be displayed by the caller.

c. When computeNetPay receives one arguments (hours worked), calculate net pay as described in step A.

d. When computeNetPay() receives two arguments (hours worked and bonus amount), calculate gross pay as in step A, add the bonus amount to the gross pay, and then calculate the net pay by applying the withholding against the gross pay.

e. Write a main() method that instantiates the class and calls both of the overloaded methods and displays their return values.

f. Name the file Pay.java and submit it using the WA2 link above.

I need to know if I am doing my code right or if I am doing it wrong and if so how can I fix the code and finish my project. This is only my second week learning Java and it is starting to bog me down because I don't know if I am doing it correctly.

public class Pay
{

    public static void main(String[] args)
    {

        double hours = 20;
        final double BONUS = 2.00;
        computeNetPay(hours);

        //computeNetPay(hours, BONUS);
    }

    public static void computeNetPay( double hours)
    {
    //double hours = 20;
    double payPerHour = 5;
    double withholdingTax = 0.25;
    double grossPay;
    double netPay;
    double tax;
    grossPay = hours * payPerHour;
    tax = grossPay * withholdingTax;
    netPay = grossPay -tax;
    System.out.println(" The Gross Pay is:$" + netPay + " with no bonus pay added.");
    }

    //public static double computeNetPay(double hours, double BONUS)
    {
        //return 
    }

}

Recommended Answers

All 4 Replies

OK, I will break the requirements down and try to explain to you.

a. Create a class named Pay that includes variables of type double that hold rate of pay per hour and withholding rate percent. Gross pay is computed as hours worked times rate of pay per hour. Net pay is calculated as gross pay minus (gross pay times withholding rate). For example, if the withholding rate is 25% and gross pay is $100, then net pay would be $100 - (100 * .25).

This part, the requirement is the first sentence only. The rest is just to explain how computation would be if you want to compute gross and net pay. Ignore them. One ambiguous from the requirement is that it does NOT tell you how to implement the class constructor... Anyway, I assume that it may not require one. The syntax of what you need to implement is a Java class is as follows.

class Pay {
  // class variables declare somewhere inside the class
  // preferably at the top of the class definition
  // you need to declare ratePerHour and witholdRate variables
  // both should be type of double

  // then class constructor goes here.
  // constructor syntax does NOT have return type
  // and the name must exactly match the class name at the top & file name
  // a default constructore is as follows...
  public Pay() { }
  // you could initiate anything inside { } if needed

  // then here goes class methods
  // the syntax for declaring class method is as follows:
  // modifiers returnType methodName(argumentListWithType) {...}
  // for example...
  public double aMethodName(int argument1, double argument2) {...}

  // you could have a main method in the class as well
  // it could be used as a quick test for the class
  // the method, however, must always be static with void return type
  public static void main (String[] args) {
    ...
  }
}

b. Create two overloaded methods named computeNetPay(), each of these methods will take different parameters and will calculate net pay based . These methods are to return the calculated value which will be displayed by the caller.

I will explain this below because this requirement asks you to implement 2 methods at the same time but one takes only 1 argument and the other takes 2 arguments (same method name but passing different number of arguments).

c. When computeNetPay receives one arguments (hours worked), calculate net pay as described in step A.

This requirement asks you to implement computeNetPay() passing in only 1 argument (hours worked). Your current implementation is somewhat correct but not all. You are supposed to use the class Pay's variable to compute, not from local variables. Look at the first requirement for class structure. Once you declare the class variables, you can use the variable anywhere in the class but main() method. Your computation is correct, but you must return the value instead of display it in the method. The syntax for the method should be similar to...

public double computeNetPay(double hours) {
  double netPay;
  // compute netPay
  ... 
  ...
  return netPay;
}

d. When computeNetPay() receives two arguments (hours worked and bonus amount), calculate gross pay as in step A, add the bonus amount to the gross pay, and then calculate the net pay by applying the withholding against the gross pay.

This is similar to the previous requirement, but the method takes 2 arguments in (hour worked and bonus amount). Similarly, do it the same as the previous requirement but a little bit different in calculation.

public double computeNetPay(double hours, double bonus) {
  double netPay;
  // compute netPay
  ... 
  ...
  return netPay;
}

e. Write a main() method that instantiates the class and calls both of the overloaded methods and displays their return values.

Once you implement everything inside the Pay class, you now test it inside the main() method. The requirement asks you to instantiate the class, and then call both methods and display the results.

public static void main(String[] args) {
  // This is how to instantiate an object of a class.
  // Follow whatever your constructor syntax is in your class.
  Pay payObject = new Pay();
  // The syntax to call a class's method is...
  // objectName.methodName();
  // In your case, you call each method and must display the result.
  // You could use System.out.println("Result: "+objectName.methodName())
  //   to instantly display the result after the call (no extra variable
  //   to store the returned value).
}

Hope this help clearing your confusion.

I seem to be understanding it a bit better but I am still having trouble with the math in the second method. Can someone please help?

Thank you in advance.

public class Pay
{
    double ratePerHour = 5;
    double witholdRate = 0.25;

    public static void main(String[] args)
    {
        Pay cls = new Pay();
        cls.computeNetPay(100);
        double payReturn = cls.computeNetPay(100,5);// Why won't this work correctly?




    }

    public double computeNetPay(double hours)
    {
        double netPay;
        netPay = hours - (hours*witholdRate);
        System.out.println("With no bonus the pay is: $" + netPay);
        return netPay;
    }

    public double computenetPay(double hours, double bonus)// how do I do the math like the instructions?
    {
        double netPay;

        netPay = hours + bonus;

        System.out.println("Amount with bonus: "+ netPay);


        return netPay;

    }
}

Before going there, I should talk about the mathematical equations first. Below is the calculation when there is no bonus (derived from the requirements).

grossPay = hours * ratePerHour
withold = grossPay * witholdRate
netPay = grossPay - withold

Now, adding bonuses to the income, it must be added before tax withold. So the only change is the gross.

grossPay = (hours * ratePerHour) + bonus

The rest is the same as before.

Thank you for all of your help. I have finished the program and am now able to say that it is running correctly and is done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.