Search code examples
javagetter-setter

How can I change a value inside of a setter?


In my code, I'm getting an "interview score" input between 0-10 from an array. I am supposed to map 0 to 0 and 10 to 100, so basically multiply the interview score by 10.

My constructor for the object is

public Person(String firstName, String lastName, double interview) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.gpa = gpa;
    this.interview = interview;
}

and my object is

Person s1 = new Person("name", "surname", 3.5, 8);

Here, 8 being the interview score and 3.5 being the GPA score (not necessarily a part of my question).

In my get and set methods I'm using

public double getInterview() {
    return interview;
}

public void setInterview(double interview) {
    this.interview = interview*10;
}

Expecting it to multiply by 10 so I can use it in my getTotalPoints method in the same class which is:

points = getGpa()*gpaWeight +  getInterview()*intWeight;

but here it takes the interview score as 8, not 80.

What can I do to fix this?

Thanks

(PS I don't really know anything about maps etc. so I don't know if it'll work here, I'd appreciate it if any answer was given in this format)


Solution

  • You are using the constructor to set the interview value instead of the setter method setInterview().

    Use setInterview() method to set the interview value or modify the constructor as below:

    public Person(String firstName, String lastName, double interview) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.gpa = gpa;
      this.interview = interview * 10;
    }