ES6 Java help getters and setters

Tell us what’s happening:
Trying to pass the learn Es6: Use getters and setters to control access to an object.

Telling i still need to :1. Define a ggetter,
2. Define a setter
3. The Thermostat should be able to be instantiated

my original attempt was extremely simmilar to the one offered in the hint solution, now it is almost an exct copy and it still will not work. The use of getters and setters i have seen online shows the getter being defined it a getName form but this seems to take me farther away from the solution
Your code so far


// Only change code below this line


/////
class Thermostat {
constructor(fTemp){
  this._fTemp=fTemp
  int fTemp

}
// getter
public void get temp(){
  return((5/9)*(this._fTemp - 32))
}
// setter
public void set temp(celsius) {
  this._fTemp=((celsius * 9)/5+32)

}
}
const l=new Thermostat

// Learning Getters and setters


// 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 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

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

Link to the challenge:

Remember, this is Javascript, not Java. You aren’t defining them correctly for Javascript. Look at the examples in the challenge to see how it is done.

Also, there is no int type in Javascript.

2 Likes

Hello!

You seem to be mixing the Java syntax with JavaScript. In vanilla ES6 there are no types nor method visibility, hence int, void and public are not recognized, causing an error in the script.

On the other hand, check the code below the initial one to see which setter and getter is expected:

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

Hope it helps :slight_smile:,

Regards.

1 Like