Confusing instructions with getter/setter control access problem

Tell us what’s happening:
I have a minor complaint but the setter part of, “In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius”, is confusing.

It makes it sound like the problem is looking for the user to convert the input temp to Celsius for the setter func instead of converting to Fahrenheit. The proper solution for the setter function is to convert the Celsius temp input back to Fahrenheit and return it. This needs to be corrected as follows, “In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Fahrenheit”.

  **Your code so far**

// Only change code below this line
class Thermostat {
constructor(temperature) {
  this._temperature = temperature;
}

get temperature() {
  return 5/9 * (this._temperature - 32);
}
set temperature(updatedTemp) {
  this._temperature = updatedTemp * 9.0 / 5 + 32;
}
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Use getters and setters to Control Access to an Object

Link to the challenge:

Actually, this is not quite correct. The setter doesn’t return a value. It just takes a value. The value it takes must already be in the proper format (or at least a format the setter understands). So if the setter requires that the value be in C then the value on the right side of the equals sign must be in C.

The class is keeping track of the temperature. You can get the temperature (returned in C) or you can set the temperature (again, in C). So I think saying create "a setter to set the temperature in Celsius.``` makes sense in this case.

1 Like

The entire purpose of getters and setters is to isolate internal object logic from the user. Therefore, there are two natural solutions to this challenge, and it goes against the point of the challenge to tell the learner to they must use a particular internal logic for their object.

The requirements are

  1. The initializer takes a temp in Fahrenheit

  2. The setter takes a temp in Celsius

  3. The getter provides a temp in Celsius

These requirements do not specify how the temp is stored inside of the object. The developer may decide to store the temp in Celsius, Fahrenheit, Kelvin, or some other system. That’s the whole point of getters and setters. The internal logic and the user facing behaviour are decoupled.

The only requirement on the setter is that it takes in a Celsius temp and uses it to update the temp stored inside of the object.

Actually, this is not quite correct. The setter doesn’t return a value. It just takes a value.

I’m not very good at using proper terminology, my solution, according to the code checker was correct, “return” was the wrong word for me to use.

The value it takes must already be in the proper format (or at least a format the setter understands). So if the setter requires that the value be in C then the value on the right side of the equals sign must be in C.

But if you look at my code below, the value to the right of the equal sign must be the conversion of the Celsius parameter, updatedTemp, back into Fahrenheit. This is the only way I was able to pass the code check:

The class is keeping track of the temperature.

I am aware.

You can get the temperature (returned in C) or you can set the temperature (again, in C).

But we aren’t setting the temp in Celsius, we take in a param that is in Celsius and convert it to Fahrenheit and then set this._temperature to the newly converted Fahrenheit value.

So I think saying create "a setter to set the temperature in Celsius.``` makes sense in this case.

I disagree, the setter takes in a value that is not provided by the user. In this case we have values provided by the code checker in order to test the user’s getter and setter functions.

Instructing the user to create "a setter to set the temperature in Celsius” makes it sound like the request is to convert the updatedTemp parameter (that’s already in Celsius, provided by the code checker) to Celsius (instead of converting to Fahrenheit), which is not the proper solution.

This is not accurate. You can store the temp internally as Celsius.

Solution 1

class Thermostat {
  constructor(fahrenheit) {
    this._temperature = 5/9*(fahrenheit - 32); // Stored in C
  }
  get temperature() {
    return this._temperature;
  }
  set temperature(celsius) {
    this._temperature = celsius;
  }
}

Solution 2

class Thermostat {
  constructor(fahrenheit) {
    this._temperature = fahrenheit; // stored in F
  }
  get temperature() {
    return 5/9*(this._temperature - 32);
  }
  set temperature(celsius) {
    this._temperature = celsius*9/5 + 32;
  }
}

Solution 3

class Thermostat {
  constructor(fahrenheit) {
    this._temperature = 5/9*(fahrenheit - 32) - 273.15; // Stored in K
  }
  get temperature() {
    return this._temperature + 273.15;
  }
  set temperature(celsius) {
    this._temperature = celsius - 273.15;
  }
}

The setter method itself can be seen as taking in a param, but when you actually use the setter in your code you are setting the temp in Celsius:

const thermos = new Thermostat(76);
thermos.temperature = 26; // this is using the setter

Because you are able to use the equals sign to set the value you are using the setter to “set the temperature in Celsius”.

Also, the setter doesn’t have to convert the C temp to F. If you store the temp internally as C then there is no conversion needed in the setter, only in the getter.

I’m not sure I understand what you mean here. A setter needs a value passed into it.

1 Like

I was under the impression that " a setter to set the temperature in Celsius" was instruction on what to do inside of the user’s setter function (specifically instruction that user input had to be converted to Celsius) and not a partial reference to what was happening in the already written challenge starting code.

Nope. Only the external (to the object) behaviour of the setter is specified. That’s why setters and getters are important and powerful.

1 Like

I didn’t realize that the challenge allowed for the user to set this._temperature inside of the constructor to Fahrenheit or Celsius (or Kelvin for that matter). I was under the impression that the stored value had to be Fahrenheit and thus the getter required conversion from Fahrenheit to Celsius and the setter required conversion from Celsius to Fahrenheit.

That combined with what I said in the quote below, I think are what threw me off:

I missed this important detail (I’ve got terrible ADHD so I tend to miss details if they don’t stand out enough or aren’t brightly colored):

Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.

Anyway thanks to both you and bbsmooth for taking the time to help clear up my confusion.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.