Search code examples
javagetter-setter

When trying to output getter, method outputs null for strings and 0 for doubles


I'm new to Java and I've been assigned a Lab to do for school. The Lab requires you to create a class with a set of instance variables with constuctors, getters, and setters. In the next class, its asks you to output variables for these variables, however, it outputs as null.

Class with getters and setters

public class ASEmployee {

        //initializes the instance variable firstname of type String
        private String firstname; 

        //initializes the instance variable last name of type string
        private String lastname;

        //initializes the instance variable monthlySalary of type double
        private double monthlySalary; 

        //makes a constructor for all three instance variables
        public ASEmployee (String userFirstname, String userLastname, 
                double userMonthlySalary) {
            setFirstname(userFirstname); 
            setLastname(userLastname); 
            setMonthlySalary(userMonthlySalary); 

        }

    //initializes a getter for firstname
    public String getFirstname() {
        return firstname; 
    }

    //initializes a setter for firstname
    public void setFirstname (String userFirstname) {
        userFirstname = firstname; 
    }

    //initializes a getter for lastname
    public String getLastname(){
        return lastname;
    }

    //initializes a setter for lastname
    public void setLastname (String userLastname) {
        userLastname = lastname; 
    }

    //initializes a  getter for monthlySalary
    public double getMonthlySalary() {
        return monthlySalary; 
    }

    //initializes a setter for monthlySalary
    public void setMonthlySalary (double userMonthlySalary) {
        if (monthlySalary >= 0.0) {
            userMonthlySalary = monthlySalary; 
        }   
    }
}

Class with the thing I'm trying to output`

public class ASPayroll {

public static void main(String[] args) {



    //creates an object for the first employee
    ASEmployee employee1 = new ASEmployee ("Bob", "Jones", 2000.00);

    //creates an object for the second employee
    ASEmployee employee2 = new ASEmployee ("Abeba", "Tefera", 3000.00); 


    //displays the monthly salary of the first employee
    System.out.printf("%s %s %s", employee1.getFirstname(), employee1.getLastname()
            , employee1.getMonthlySalary());


    //displays the monthly salary of the second employee
    System.out.printf("%n%s %s %s", employee2.getFirstname(), employee2.getLastname(),
            employee2.getMonthlySalary());

And this outputs the strings as "null" and the doubles as "0.0"


Solution

  • Your setters are wrong:

    // userLastname is the parameter you are passing in.  lastname is the class 
    // variable.  You are setting the parameter to the value of the uninitialized
    // value of lastname.  userLastname then is thrown away the setter function ends.
    userLastname = lastname; 
    

    should be:

    // Sets your class variable to the value of the parameter being passed in.
    lastname = userLastname; 
    

    You also have this wrong for firstname and monthlySalary.