-2

I've created 2 classes: Pay and PaycheckCalculator. Here is the method that does the computations:

public class Pay {   
    private double hoursWorked;
    private double rateOfPay;
    private double withRate;
    private double grossPay;
    private double netPay;

    public Pay ()
    {
        withRate = 15;
        rateOfPay = 5.85;
    }

    public void computeNetPay(double hours, double ratePay, double rateWith)
    {
        grossPay = hours * ratePay;
        double newAmt = grossPay*rateWith/100;
        netPay = grossPay - newAmt;       
    }

    public void computeNetPay(double hours, double ratePay)
    { 
        grossPay = hours * ratePay;
        double newAmt = grossPay*withRate/100;
        netPay = grossPay - newAmt;       

    }

    public void computeNetPay(double hours)
    {
        grossPay = hours * rateOfPay;
        double newAmt = grossPay*withRate/100;
        netPay = grossPay - newAmt;        
    }       
}

And here is the one that calls and displays the results, unfortunately, I cannot get it to run based on how the book requires it to run.

public class PayCheckCalculator {
    public static void main(String[] args) {

        Pay employee1 = new Pay(37.00, 12.00, 15.00);
        Pay employee2 = new Pay (25.00, 11.00);
        Pay employee3 = new Pay (15.00);
        display(employee1);
        display(employee2);
        display(employee3);
    }

    public static void display (Pay paycheck)
    {
         System.out.println("Employee pay is" + Pay.computeNetPay);
    }
}

Any tips will help me along in my re-education process.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Jack Parker
  • 547
  • 2
  • 9
  • 32
  • Use `paycheck.computeNetPay`, because it is an instance method. – August Jul 27 '14 at 20:04
  • What is the exact problem? – BitNinja Jul 27 '14 at 20:06
  • possible duplicate of [Calling non-static methods from other classes](http://stackoverflow.com/questions/14567360/calling-non-static-methods-from-other-classes) – Jeroen Vannevel Jul 27 '14 at 20:09
  • Spend some time searching first before asking your question; you'll notice on the right side that there are 10 questions readily available with almost your exact same title. – Jeroen Vannevel Jul 27 '14 at 20:10
  • I don't understand how all the `new Pay(...)` lines are compiling. You've only defined a `Pay` constructor that takes no arguments, but you're trying to construct with one, two, or three arguments. Did you define other constructors that you left out? – ajb Jul 27 '14 at 20:23
  • @Jerron I did search, I didn't find what i was looking for and asked. Nice to see asking simple questions can get me negative feedback in this place, good job camp counselor of stack overflow. No wonder I try to avoid asking questions in this place. – Jack Parker Jul 27 '14 at 20:42

2 Answers2

0

You need an instance of an object to be able to call a method from it unless the method is declared static. As mentioned above, you can call:

paycheck.computeNetPay(whatever params you require)

This will call the method from the class paycheck that you pass into the display method.
If you want to create an object first, the method need not be declared static and can be called like this:

p1.computeNetPay(whatever params you require)

where p1 is an instance of the class Pay. Since here the object instance of the class is called paycheck, this is the object you are calling the (un-necessarily) static method from.
Also when you write Pay employee1 = new Pay(37.00, 12.00, 15.00); this looks like a constructor You should not be using this as a constructor. A constructor is to create instances of classes, it looks here like you are trying to pass parameters from your method into a constructor, and with only values, no types.

The only constructor you have defined for the Pay class takes no arguments, so you cannot pass in any to it when you want to create an object.

Here is a good explanation of how constructors work.

This should start you in the right direction, use the same approach for the other methods but with more parameters.

  public class Employee {

     // These should be public unless you have getters and setters  
     public double hoursWorked;
     public double rateOfPay;
     public double withRate;
     public double grossPay;
     public double netPay;

     public Employee()
     {
         withRate = 15;
         rateOfPay = 5.85;
     }

     // Return the double netPay from this method
     public double computeNetPay(double hours)
     {
         grossPay = hours * rateOfPay;
         double newAmt = grossPay*withRate/100;
         netPay = grossPay - newAmt;        
         return netPay;
     }       

     public static void main(String []args){
        Employee Ben = new Employee();
        System.out.println("Ben gets paid " + Ben.computeNetPay(8.0));
       }
     }
  • I thought I had to write the call based on the class the method was in, as in pay.compute – Jack Parker Jul 27 '14 at 20:43
  • Would I be making my life easier putting all this in one class? – Jack Parker Jul 27 '14 at 20:43
  • OOP is about managing the flow of dependency between objects, if you think this justifies more than one class then you can do that. You make a static method call based on a class, static basically means "requires no object", so the method belongs to the class and requires no instance. If the method is not static, you must first create an instance of it. – Ḟḹáḿíṅḡ Ⱬỏḿƀíé Jul 27 '14 at 20:47
  • If it were me, I would maybe have an Employee class and create instances of the class for each employee. You would create one like Employee Ben = new employee(); and then call the methods from each instance. – Ḟḹáḿíṅḡ Ⱬỏḿƀíé Jul 27 '14 at 20:51
  • See i thought of that,but the way the book phrases the question didn't imply i should but it seemed like that would be much easier. – Jack Parker Jul 27 '14 at 21:22
  • There are usually many ways to solve a problem, sometimes never an easy way. The way I have coded above will work fine but if you need two classes and need a static method then you can call the method using Employee.computeNetPay(x); where x is the number of hours. – Ḟḹáḿíṅḡ Ⱬỏḿƀíé Jul 27 '14 at 21:32
-1

Non-static methods can only be called on objects, for example:

paycheck.computeNetPay(...);
grexter89
  • 1,091
  • 10
  • 23