Using the Mutator Method in client program

474 views Asked by At

I just need some clarification whenever i use the Mutator Method in the client program okay so i my employee class set up and i was taught that my variable should be private double salary then have my Accessors then the Mutators and it should be public void setSalary (double newSalary) and inside it have salary = newSalary

Now, i'm coding this in eclipse and i'm in my client program so i put Employee.setSalary(200.00); which i was taught was the right way and the book we use in class also shows it like this...

However, Eclipse tells me that my mutator should be public static void setSalary (double newSalary); and that my initial variable should be private static double salary. why is it saying that if the book and my professor say it's the other way?

So here is my code for my class

    import java.text.DecimalFormat;

    public class Employee {
    private String Last_Name;
    private String First_Name;
    private int Years_in_Company;
    private double Salary;
    private char Status;
    private String Section;

    public Employee(){
        Last_Name = "unknown";
        First_Name = "unkown";
        Years_in_Company = 0;
        Salary = 0.0;
        Status = '\u0000';
        Section = "unkown";
    }

    public String getLast_Name(){
        return Last_Name;
    }

    public String getFirst_Name(){
        return First_Name;
    }

    public int getYears_in_Company(){
        return Years_in_Company;
    }

    public double getSalary(){
        return Salary;
    }

    public char getStatus (){
        return Status;
    }

    public String getSection(){
        return Section;
    }

    public void setLast_Name(String newLast_Name) {
        Last_Name = newLast_Name;
    }
    public void setFirst_Name(String newFirst_Name){
        First_Name = newFirst_Name;
    }
    public void setYears_in_Company(int newYears_in_Company){
        if( newYears_in_Company >= 0)
            Years_in_Company = newYears_in_Company;
            else{
                System.err.println ("Years in company cannot be negative");
                System.err.println ("Value not changed");
            }
    }
    public void setSalary (double newSalary){
        if ( newSalary >= 2000.00){ //good practice to add braces to `if`condition.
            Salary = newSalary;
              }
        else{
            System.err.println("Salary cannot be less than $2,000.00");
            System.err.println("Value not changed");
        }
    }
    public void setStatus(char newStatus){
        if (newStatus == 'a' || newStatus == 'r'){
            Status = newStatus;
                }
        else{
            System.err.println ("Status cannot be anything other than a or r");
            System.err.println ("Value not changed");
        }
    }
    public void setSection (String newSection){
        Section = newSection;
    }
    public void display (){
        System.out.println ( Last_Name + First_Name + Years_in_Company + Salary + Status + Section);
    }
    public String toString (){
        DecimalFormat SalaryFormat = new DecimalFormat ("#0.0");
        return "Name: " + Last_Name
                + ", " + First_Name
                + ", Years in Company: " + Years_in_Company
                + ", Salary: " + SalaryFormat.format ( Salary )
                + ", Status: " + Status
                + ", Section: " + Section;

    }
    public boolean equals( Object o){
        if (!(o instanceof Employee) ){
            return false;
                }
        else{
            Employee objEmployee = (Employee) o;
            if ( Last_Name.equals (objEmployee.Last_Name) &&
                    First_Name.equals (objEmployee.First_Name) &&
                    Math.abs(Years_in_Company - objEmployee.Years_in_Company) < 0.0001 &&
                    Math.abs(Salary - objEmployee.Salary) < 0.0001 &&
                    Status == objEmployee.Status &&
                    Section.equals(objEmployee.Section))

                    return true;
            else
                    return false;
        }
    }

}

i just want to know why Eclipse tells me that my mutator should be public static void setSalary (double newSalary); and that my initial variable should be private static double salary. why is it saying that if the book and my professor say it's the other way?

0

There are 0 answers