简体   繁体   中英

How do i perform a calculation within a setter method in java?

I have two classes within a java project, one is the Employee1 class and the other is the info class. Within the Employee 1 class I have this code:

public class Employee1 {
    String employeeName;
    int hoursWorked;
    double rateOfPay;
    double wages;




public Employee1(String name, int hours, double pay) {
    employeeName=name;
    hoursWorked=hours;
    rateOfPay=pay;

}


public String getName() {
    return employeeName;
}

public void setName(String xName) {
    employeeName = xName;
}


public double getHours() {
    return hoursWorked;
}


public void setHours(int xHours) {
    hoursWorked = xHours;
}


public double getPay() {
    return rateOfPay;
}


public void setPay(double xPay) {
    rateOfPay = xPay;
}

public double calculateWages  () {
wages= hoursWorked * rateOfPay; 
return wages;
} 

public void print() {

    System.out.println("name:"+ employeeName);
    System.out.println("hours " + hoursWorked);
    System.out.println("Wages Earned"+  wages);
}
}

Within my info class I have this code:

public class Info {

    public static void main(String[] args) {
        Employee1 x= new Employee1("Sarah",40,7.25);
        Employee1 y=new Employee1("Bob",30,8.00);
        Employee1 z= new Employee1("Todd",26, 8.25);



x.print();  


    }

}       

My problem right now is that I am attempting to create a calculation for wages within my Employee1 class as you can see with my calculateWages method. However, whenever I call my print method for my existing employee x the wages always come out to be 0.0 and I am unsure why this is happening. I have preexisting variables for each of my Employee1 objects and I am still getting a 0.0 value for all of their wages but the rest of their information is printed correctly. Can anyone help me with this issue? Thank you!

In the code you provide there is not a setter method, instead there is a getter one. And yes, you can made calculation in both, getter and setter.

public void setAmount(int amount){
  this.amount = quantity * price;  
} 

public void getAmount(){
  return this.amount + otherProperty;  
} 

Lets start with this:

I'm new to programming but I know that this type of method is known as a setter.

No it isn't.

In OO, the code (mostly) consists of classes which consist of state (fields) and methods that (typically) operate on that state.

By convention ...

  • A setter is a method that sets a field to a new value provided as a method argument.
  • A getter is a method that returns the value of a field.

Your method is neither a setter or a (pure) getter:

  • It is not a setter because it doesn't set wages to a supplied value. (It is doing the calculation based on previously supplied values.)

  • You could view it as a getter for the wages field, but it is "impure" in that it updates the field as well as returning its value.


Every time I go to print out the "wages" variable it just gives me a zero and nothing else, I have the other variables hoursWorked & rateOfPay defined in the main

We cannot explain this without seeing the rest of your code. However, I suspect that the problem is one of the following:

  1. Either of hoursWorked or rateOfPay is zero ... because they are not being correctly set / initialized to non-zero values.

  2. The calculateWages method is not being called.

  3. You have multiple instances of the class that defined this method ... and your code is looking at the wrong one.

  4. Possibly ... some of the variables involved have been incorrectly declared as static .s


UPDATE - Now that I see your code, the reason that wages is zero is that your code doesn't call calculateWages .

In the best practice of OOP your method should have two arguments: eg

public class Answer1 {

    public static double calculateWages  (int hoursWorked, double rateOfPay){
        return hoursWorked * rateOfPay;
    }

    public static void main(String[] args) {

        System.out.println(calculateWages(6, 5.24));

    }

}

You can pass those values to the calculateWages () method. Try this

public double calculateWages  (int hoursWorked, double rateOfPay) {
     return hoursWorked * rateOfPay;
}

You may want to simplify this by creating a class, outside of the main class with the main method, that does this ONE specific job of calculating a wage. We call this "isolation" in programming, and we want to isolate one class from another and make sure each class only does one particular job. Here is an example of a class that will get the total wage:

public class WagesCalc {

      public static double calculateWages(int hoursWorked, double hourlyWage) {
            double wage = (double)hoursWorked * hourlyWage;
            return wage;
            }

}

In the code above we can create a class that calculates the wages called WagesCalc. Within it, we can create a method called calculate wages. You can add hours worked, and the hourly wage as the arguments for the class. We then add a variable, and then return an the wage. We make this method static so that we can return it without an instance of the class being created. Here is the code in the main method:

public class App {

       public static void main( String[] args )throws IOException{

            double wages = WagesCalc.calculateWages(23, 23.50);
            System.out.println("My total wage is $" + wages);


      }
}

And here is the output:

My total wage is $540.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM