You are given the following interface: public interface Movable { String move (S
ID: 3557251 • Letter: Y
Question
You are given the following interface:
public interface Movable
{
String move (String movingObject, char movement);
}
Based on the Movable interface create a class MovableObject that implements it. The parameter movingObject can receive any String value (i.e. car, truck, bike) and the parameter movement can receive the characters 'l', 'r', 's' (left, right or stopped). Depending on the value of movingObject and movement the corresponding String should be returned when the move method is called (i.e. "car is moving right" for movingObject = "car" and movement = "r", "truck is stopped" for movingObject = "truck" and movement" = "s", "bike is moving left" for movingObject = "bike and movement = "l").
Please help me program this. I must use Interface Movable and class MovableObject. (More classes can be used however the following must be met.)
Thank you, I will run the program and whichever works exactly the way it should will receive the points (for obvious reasons).
Explanation / Answer
public interface Movable {
String move (String movingObject, char movement);
}
public class MovableObject implements Movable {
String movingObject;
Character movement;
public String move(String movingObject, char movement) {
String direction="";
if(movement=='s')
direction="Stopped";
if(movement=='l')
direction="Left";
if(movement=='r')
direction="Right";
return "The "+movingObject +" is moving "+direction;
}
public static void main(String[] args)
{
Movable movableObject=new MovableObject();
System.out.println(movableObject.move("Bike", 'l'));
System.out.println(movableObject.move("Car", 'r'));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.