Question

I'm coding a fairly simple program that simply outputs 2 employee's Names, their salary, and a 10% raise to that salary. I have 2 issues: 1) The salary prints as '$0.000000' 2) Ii cannot get the raise method to work properly

Here's my code:

public class Employee {

    // instance variable
    private String name;
    private  String lastName;
    private  double salary;

    // setters
    public  String getLastName() { return lastName; }
    public  String getName() { return name; }
    public  double getSalary() { return salary; }

    public  void raise(double raise) { salary = salary + (salary * .1); }

    // getters
    public  Employee(String name, String lastName, double salary) {
        this.name = name;
        this.lastName = lastName;

        if (salary > 0.0) {
          this.salary = salary;
        }
    }
} 

public class EmployeeTest {

    public  static void main(String[] args) {

        Employee raise1 = new Employee("Betty", "Jones", 4000.0);
        Employee raise2 = new Employee("Sally", "Mae", 6000.0);

        // Print statements for the employee's name and salary
        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" + "Salary: $%f", raise1.getName(), raise1.getLastName(), raise1.getSalary());

        // THIS IS WHERE I'M HAVNG TROUBLE
        System.out.printf("Her raise will be: %d", raise1.raise(salary));

        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" + "Salary: %f", raise1.getName(), raise1.getLastName(), raise1.getSalary());

        raise2.raise(salary);
    }
}

Thanks in advance for the help!

Was it helpful?

Solution

The reason salary printed $0.00000 is because you've placed if statement for some reason instead of just setting salary you passed to constructor. Second thing, void methods does not return anything that's the reason raise method did not work the way you wanted. I fixed it for you so take a look, hopefully I helped you understand where you went wrong and if you have any questions feel free to ask I'm here for you.

Employee Test class:

public class EmployeeTest {

    public static void main(String[] args) {

        Employee raise1= new Employee("Betty","Jones",4000.0);
        Employee raise2= new Employee("Sally","Mae",6000.0);

        //Print statements for the employee's name and salary
        System.out.printf("Employee #1\nFirst Name: %s\nLast Name: %s\n\n" +
                "Salary: $%.2f",
                raise1.getName(),
                raise1.getLastName(),
                raise1.getSalary());

        System.out.printf("\nHer raise will be: $%.2f",
                raise1.raise(raise1.getSalary()));


        System.out.printf("\n\nEmployee #2\nFirst Name: %s\nLast Name: %s\n\n" +
                "Salary: $%.2f",
                raise2.getName(),
                raise2.getLastName(),
                raise2.getSalary());

        System.out.printf("\nHer raise will be: $%.2f\n",
                raise2.raise(raise2.getSalary()));

    }
}

Employee class:

public class Employee {

   //instance variable
   private String name;
   private String lastName;
   private double salary;

    //getters
    public String getLastName() {
        return lastName;
    }
    public String getName() {
        return name;
    }
    public double getSalary() {
        return salary;
    }

    public double raise(double raise){
        salary = salary + (salary*.1);
        return salary;
    }

    //constructor
    public Employee(String name, String lastName, double salary){

        this.name = name;
        this.lastName = lastName;
        this.salary = salary;

    }

}

Print output from the program above:

Employee #1

First Name: Betty

Last Name: Jones

Salary: $4000.00

Her raise will be: $4400.00

Employee #2

First Name: Sally

Last Name: Mae

Salary: $6000.00

Her raise will be: $6600.00

OTHER TIPS

When you do

Employee raise1= new Employee("Betty","Jones",4000.0);

you call the constructor of Employee class that have the following condition:

 if (salary <0.0){
     this.salary = salary;
 }

Since 4000.0 is more than 0, it never gets assigned.

Your raise() method's return type is void, which means that it will not return anything at all. Change the return type to double, and simply typing return this.salary at the end should work fine. It should look like this:

public double raise(double salary)
{
    this.salary = salary + (salary * 0.1); 
    return this.salary;
}

The this keyword references the instance of the object that you are using. Put it simply, you probably want to reference the classes salary variable, rather than the parameter passed in the method, otherwise it will reference the parameter passed in.

And you also need call this.salary = salary; in your constructor, because the only way it was assigned before is if salary is below zero, but 4000 is not below zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top