Urgent help needed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Urgent help needed

b. Implement the setter in such a way that negative values (e.g., -15km/h) and values higher than 260 km/h cannot be assigned to instance variable speed. If someone tries to assign such a “forbidden” value, the setter should do nothing (old value of instance variable speed should remain unchanged). https://code.sololearn.com/chnk2qKs892f/?ref=app

13th Mar 2018, 5:22 PM
Djamali Dollison
Djamali Dollison - avatar
4 Answers
+ 2
Perhaps you can try this (untested): if(value > 0 && value < 260) { this.speed = value; } FYI, in your code there is no member in the class named "speed" so codes that refers to "this.speed" fires error, maybe you forget it. Hth, cmiiw
13th Mar 2018, 6:32 PM
Ipang
0
i need help with my setter public int Speed { get { return this.speed; } set { if (Speed > 0) { this.speed = value; } else if (Speed < 260) { speed = value; } else { value = speed; speed = value; }
13th Mar 2018, 5:23 PM
Djamali Dollison
Djamali Dollison - avatar
0
i dont know how to let the value remain unchange if it doesnt satify the condition
13th Mar 2018, 5:24 PM
Djamali Dollison
Djamali Dollison - avatar
0
i have an instance variable... in place but my question was solved thank u
13th Mar 2018, 6:35 PM
Djamali Dollison
Djamali Dollison - avatar