+ 1
Whats wrong in this please help to solve
public class Vehicle { private String speed; public String getSpeed(){ return speed; } public String setSpeed(String x){ this.speed=x; } } class program { public static void main(String[]args){ Vehicle duke=new Vehicle(); Vehicle fz=new Vehicle(); duke.setSpeed ("ten"); System .out.println (duke.getSpeed()); fz.setSpeed("five"); System.out.println (fz.getSpeed ()); } }
1 Answer
+ 6
The error was a lack of return statement in setSpeed. Methods that don't return anything should be declared "void," and they don't have to contain a return statement. This should work:
public class Vehicle {
private String speed;
public String getSpeed(){
return speed;
}
//changed metbod to "void" because it doesn't return anything
public void setSpeed(String x){
this.speed=x;
}
}
class program {
public static void main(String[]args){
Vehicle duke=new Vehicle();
Vehicle fz=new Vehicle();
duke.setSpeed("ten");
System.out.println(duke.getSpeed());
fz.setSpeed("five");
System.out.println (fz.getSpeed());
}
}