Home > Blockchain >  How can I change a value inside of a setter?
How can I change a value inside of a setter?

Time:06-05

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)

CodePudding user response:

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;
}

CodePudding user response:

You are never calling the setInterview function. If you don't call it then how the value will change. Assuming you are only passing the values using constructor. I would suggest o to call the setInterview inside the constructor.

public Person(String firstName, String lastName, double interview) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.gpa = gpa;
    setInterview(interview); // or you can simply assign the value here 
}

CodePudding user response:

You can immediately initialize the value of the interview, if this parameter is used only in the form in which you wrote it.

this.interview = interview * 10;

And you will no longer need the setInterview() method

CodePudding user response:

The problem is that between your constructor and setter there is inconsistency on the scale of representation of the Interview attribute.

In the constructor:

this.interview = interview;

so here you work in a range from 0 to 10

Whereas in the setter

this.interview = interview*10;

so here you work in the range from 0 to 100

You can either set the value in the 0-100 scale and use it that way, or set it in the 0-10 scale and each time you use it you have to remember to map it to the 0-100 scale. I think the best solution is the former.

Regarding mapping from one range to another, you can take inspiration from this answer: Mapping a numeric range onto another

  • Related