+ 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
4 Respuestas
+ 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
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;
}
0
i dont know how to let the value remain unchange if it doesnt satify the condition
0
i have an instance variable... in place but my question was solved thank u